Search code examples
c#.netprocessexit-code

What is the equivalent of EXIT_SUCCESS and EXIT_FAILURE in C#?


I'm starting a process and want to check its exit code for success or failure.

Process myProcess = new Process();

myProcess.Start();

myProcess.WaitForExit();

// What should i put instead of EXIT_SUCCESS ?
if (myProcess.ExitCode == EXIT_SUCCESS) 
{
  // Do something
}

EXIT_SUCCESS doesn't seem to exist, is there an equivalent or is the canonical way in C# to just check against zero ?


Solution

  • From C/C++ code normally those are defined as constants in some include header that makes use of those. So you typically have something like this:

    #define EXIT_SUCCESS 0
    

    And similar definitions for some other possible return codes. After all, those "exit codes" are just numeric values that are defined as constants by some include file, but not a language construct. As C# lacks includes and header files, you may want to define them manually:

    private const int EXIT_SUCCESS = 0
    

    And then just use in your code as any other constant. So your sample code will now compile and work as expected

    if (myProcess.ExitCode == EXIT_SUCCESS) 
    {
      // Do something
    }