Noob file.copy question having issues copying .exe files C#
I am trying to copy a .exe file from the temp directory to the desktop, however when I do so it just creates a new .exe which has no data in it and is 0 KB in size. I tested this syntax with a .txt file and it copied it completely, it just refuses to copy .exe files for some reason. I tried executing it using the the string path
to make sure it was grabbing the correct location and that worked, executing the helloworld.exe program in the temp directory. Also I do not get any compiler errors, I am on windows 7 x86. Thanks!
string path = Path.GetTempPath() + "helloworld.exe"; // grabing the temp directory
string path2 = "C:\\us开发者_开发技巧ers\\grant\\desktop\\helloworld.exe"; //this is where i want
//it to copy to
File.Copy(path, path2, true); //copying the 2 paths
Process.Start(path); //running the .exe in the temp directory to test if it works
Is the .exe
in use during the copy?
Alternatively, any chance AV software is stopping your app making .exe
copies?
Remember, with File.Copy in C# you need to make sure that the destination file doesn't exist -- File.Copy will fail if you try to copy to an existing file. So, that could be contributing.
A try/catch block could be handy too:
try
{
string path = Path.GetTempPath() + "helloworld.exe";
string path2 = "C:\\users\\grant\\desktop\\helloworld.exe";
File.Copy(path, path2, true);
}
catch(Exception e)
{
Console.WriteLine("{0} exception caught.", e);
}
Try renaming it before and after copy to a .txt to see if it relates to .exe, even though it seems that something else is wrong in your environement
精彩评论