Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the C# code.

any thoughts?

Also how can I give the processes a callback method for when it has finished?

Thanks

like image 768
Aly Avatar asked Mar 09 '26 19:03

Aly


1 Answers

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.

like image 194
Ani Avatar answered Mar 12 '26 08:03

Ani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!