I am trying to run a batch script from within a c sharp program the code I am using is shown below:
Process proc = new Process();
proc.StartInfo.FileName = "G:\\Media\\Downloads\\print.bat";
proc.Start();
The script is simple (for testing purposes) and contains one line:
echo hello > output.txt
When I run the script from windows explorer it works, but does not when run from the C# code.
any thoughts?
Also how can I give the processes a callback method for when it has finished?
Thanks
It works fine for me. I’m guessing what’s happening is that when you execute the batch file programmatically, you’re expecting the output file to be created in the
Downloadsfolder, when it’s actually being created in the application’s folderEither fully qualify the output file-path in the batch file or change the working directory of the launched process, like this:
As for your question about receiving notification when the process has finished, you can use the
WaitForExitmethod or theExitedevent on theProcessobject.