Reputation: 15756
The problem is that the WaitForExit does not wait until the batch file quits. It comes back almost right away.
I'm starting my batch file as follows:
ProcessStartInfo startInfo = new ProcessStartInfo(batchFile);
startInfo.UseShellExecute = true;
startInfo.Arguments = arguments;
using (Process p = Process.Start(startInfo))
{
p.WaitForExit();
}
I tried with and without UseShellExecute
.
Upvotes: 3
Views: 3044
Reputation: 1291
You could try running cmd with a "/c yourbatchfile" as command line arguments instead.
Upvotes: 1
Reputation: 1258
It seems that you can do it redirecting the StdOut
and read it until it closes.
Took this idea from this similar question.
Adapting your snippet, that would be:
ProcessStartInfo startInfo = new ProcessStartInfo(batchFile);
//startInfo.UseShellExecute = true;
startInfo.Arguments = arguments;
startInfo.RedirectStandardOutput = true;
Process p = Process.Start(startInfo);
String output = proc.StandardOutput.ReadToEnd();
Upvotes: 0