101010
101010

Reputation: 15756

Starting a batch file in C# and then waiting for it to exit before continuing

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

Answers (2)

mageos
mageos

Reputation: 1291

You could try running cmd with a "/c yourbatchfile" as command line arguments instead.

Upvotes: 1

J.Hudler
J.Hudler

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

Related Questions