Noob file.copy question having issues copying .exe files C#

一世执手 提交于 2019-12-11 04:26:07

问题


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:\\users\\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

回答1:


Is the .exe in use during the copy?

Alternatively, any chance AV software is stopping your app making .exe copies?




回答2:


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);
    }



回答3:


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



来源:https://stackoverflow.com/questions/5972031/noob-file-copy-question-having-issues-copying-exe-files-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!