2

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
Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
llk
  • 2,501
  • 7
  • 36
  • 43
  • Don't see why that wouldn't work. No exceptions thrown? – Cory Nelson May 12 '11 at 00:25
  • 1
    Is the exe blocked? Right-click on it...Properties to check. If it has an 'Unblock' button, click that. Also make sure the exe isn't running. – keyboardP May 12 '11 at 00:26
  • I am on Windows 7 x86, I am using VC2010 Professional and I am on an Administrator account. The permissions are all checked for Administrator and my current user and the program is unblocked. I checked that the .exe is not running too. – llk May 12 '11 at 00:30
  • 4
    Try a copy from a non-temp directory or a non .exe file to see it's a more general issue, or just copying a .exe from the temp directory? – Joe May 12 '11 at 00:33
  • 1
    I've tried your code, without any changes except the file path, and it copied the exe fine. Like Joe says, try another exe from another folder. – keyboardP May 12 '11 at 00:34
  • Thanks Joe and keyboardP, That worked. It copied it 100%. Does anybody have an idea why a sub directory (desktop\test\helloworld.exe) on my desktop worked instead of the temp folder? – llk May 12 '11 at 00:36

3 Answers3

1

Is the .exe in use during the copy?

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

Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185
1

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);
    }
Joe
  • 800
  • 4
  • 15
0

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

sp4ke
  • 549
  • 2
  • 12
  • Ok, I tried that and it now creates a .txt file with no data at all. Usually when you open a .exe in text format you get gibberish and a header saying this program cannot be run in DOS mode, but I get nothing. Is it just NOT copying it and creating a new file? I am not getting any errors... – llk May 12 '11 at 00:34