Running bat script from C#
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 t开发者_Go百科he 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 Downloads
folder, when it's actually being created in the application's folder
Either fully qualify the output file-path in the batch file or change the working directory of the launched process, like this:
proc.StartInfo.WorkingDirectory = @"G:\Media\Downloads\";
As for your question about receiving notification when the process has finished, you can use the WaitForExit
method or the Exited
event on the Process
object.
精彩评论