1

I have written a small batch file that copies an exe from solution to the system32 folder.

copy "blah.exe" "%systemroot%/System32"

The batch file works fine and copies the exe if ran from the desktop by double clikcing (placed exe on the desktop as well)

However, I tried doing that from Windows Application by:

Process.Start("sample.bat");

(EXE file and batfile -> Properties -> Output to Copy Always) The cmd window does come up, but the .exe file is not there in the destination directory. What am I missing here?

ChrisF
  • 134,786
  • 31
  • 255
  • 325
user1240679
  • 6,829
  • 17
  • 60
  • 89
  • Why do you expect `blah.exe` to be in the output folder? – Oded Apr 09 '12 at 10:15
  • Messing around with system32 is a bad idea... the more recent windows version have several security measures (among other UAC) to protect against this... what exactly are you trying to do ? – Yahia Apr 09 '12 at 10:19
  • @Yahia: Trying to copy a sysinternal utility to System32 to be abel to access it from command line – user1240679 Apr 09 '12 at 11:16
  • @user1240679 the correct way to do this is NOT to put it into system32 BUT to add the relevant path to `PATH` ! – Yahia Apr 09 '12 at 11:41

1 Answers1

0

in your batch file change path to that particular folder where you have blah.exe, change to the particular drive and then to particular folderlets say your source folder is C:\test then type cd\test in the batch file, it should be something like:

C:
cd\test
copy "blah.exe" "%systemroot%/System32"

or use copy with complete path e.g

copy "C:\test\blah.exe" "%systemroot%/System32"

EDIT: To copy using CMD try:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new       System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C copy /b blah.exe %systemroot%/System32";
process.StartInfo = startInfo;
process.Start();

Edit 2: Or for batch file

 System.Diagnostics.Process.Start("cmd", "/c sample.bat");
Habib
  • 219,104
  • 29
  • 407
  • 436
  • I have the exe and the batch file in the same folder. I tried this on putting both `batch` file and `exe` on desktop, and just executing the above batch file, which works fine. In this case, I did not need to change the source directory to `cd Users/someuser/Desktop`. Putting the full path on batch file here can work, but when I would deploy the project, the path for this `cd` in batch file would change, in which case nothing would happen. – user1240679 Apr 09 '12 at 11:10
  • If I run the batch file directly from the bin/debug output folder, it still works adn the exe gets copied to `System32`. It's just that this is not running from `Process.Start` – user1240679 Apr 09 '12 at 11:12
  • @user1240679, just check the edited answer, also check out this question http://stackoverflow.com/questions/1469764/c-sharp-run-command-prompt-commands – Habib Apr 09 '12 at 11:44
  • @habis.osu: I am still wondering why is that the problem with batch file, rather than passing arguments. – user1240679 Apr 09 '12 at 12:03
  • @user1240679 you could specify batch file in process.start like System.Diagnostics.Process.Start("cmd", "/c mybatch.bat"); but you have to start process against cmd – Habib Apr 09 '12 at 12:09