Noob file.copy问题复制.exe文件时出现问题C#

时间:2011-05-12 00:22:24

标签: c# file copy executable

我正在尝试将.exe文件从临时目录复制到桌面,但是当我这样做时,它只创建一个新的.exe,其中没有数据,大小为0 KB。我用.txt文件测试了这个语法并且它完全复制了它,它只是因为某种原因拒绝复制.exe文件。我尝试使用string path执行它以确保它正在抓取正确的位置并且工作正常,在temp目录中执行helloworld.exe程序。此外,我没有得到任何编译器错误,我在Windows 7 x86上。谢谢!

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

3 个答案:

答案 0 :(得分:1)

复制期间是否正在使用.exe

或者,AV软件是否有机会停止您的应用制作.exe份副本?

答案 1 :(得分:1)

请记住,使用C#中的File.Copy,您需要确保目标文件不存在 - 如果您尝试复制到现有文件,File.Copy将失败。所以,这可能有所贡献。

try / catch块也很方便:

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

答案 2 :(得分:0)

尝试在复制到.txt之前和之后重命名它以查看它是否与.exe相关,即使您的环境中似乎有其他错误

相关问题