prosseek
prosseek

Reputation: 190659

Checking the result of running process with C#

This post has the following code.

Process p = new Process();
StringBuilder sb = new StringBuilder("/COVERAGE ");
sb.Append(exeFileName);
p.StartInfo.FileName = "vsinstr.exe";
p.StartInfo.Arguments = sb.ToString();
p.Start();
p.WaitForExit();
// Look at return code – 0 for success

The comment says I need to check the return code, but p.WaitForExit() doesn't return anything.

Upvotes: 1

Views: 4274

Answers (4)

Nikola Radosavljević
Nikola Radosavljević

Reputation: 6911

Just check

p.ExitCode

after process exits. Process.ExitCode Property

Upvotes: 0

Nicholas Carey
Nicholas Carey

Reputation: 74177

After the process completes, the property ExitCode of the System.Diagnostics.Process object instance should contain the program status code.

Upvotes: 0

itowlson
itowlson

Reputation: 74802

For Q1, check the Process.ExitCode property.

For Q2, exit codes for success and failure are defined by the called process itself, but conventionally 0 indicates success and anything else indicates failure.

Upvotes: 6

Rob Fonseca-Ensor
Rob Fonseca-Ensor

Reputation: 15621

just look at the ExitCode property to see if the process exited happily or not.

With respect to a running process, you can watch the standard error stream to see if any messages are printed there. Chances are they will represent some kind of problem, but this is even more implementation-dependant than the exit code.

Upvotes: 2

Related Questions