简体   繁体   中英

Copy file to debug folder

I programed an application that should copy file of database (bazarganidb.db) from driver d into debug folder.if i want copy this into some another location it has not any problem but when i do it for debug folder nothing happen, no file will be delete or copy.whats my wrong? its my code:

private void button3_Click(object sender, EventArgs e)
{
    try
    {
        string OriginalFile = @"D:\bazarganidb.db";
        string FileToReplace = @"E:\BazarganiProj2\BazarganiProj\bin\Debug\bazarganidb.db";

        if(File.Exists(FileToReplace))
            File.Delete(FileToReplace);
        File.Copy(OriginalFile, FileToReplace);
    }
    catch (Exception k)
    {
        Console.WriteLine(k);
    }
}

That is a bad practice to hard code the path, use Reflection to reference the path:

  string FileToReplace = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(
  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "bazarganidb.db");

also check your permissions to that folder, try to create file or folder manually and it will indicate if you have a permission block.

also, if you want to override the file, use the designate constructor with the 3 arguments, the third argument must be set to true if you want to override the existing file:

File.Copy(OriginalFile, FileToReplace,true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM