How do I specify the exit code of a console application in .NET?

MrDatabase picture MrDatabase · Oct 1, 2008 · Viewed 323.5k times · Source

I have a trivial console application in .NET. It's just a test part of a larger application. I'd like to specify the "exit code" of my console application. How do I do this?

Answer

TheSoftwareJedi picture TheSoftwareJedi · Oct 1, 2008

3 options:

  • You can return it from Main if you declare your Main method to return int.
  • You can call Environment.Exit(code).
  • You can set the exit code using properties: Environment.ExitCode = -1;. This will be used if nothing else sets the return code or uses one of the other options above).

Depending on your application (console, service, web app, etc) different methods can be used.