Coder Perfect

In.NET, how can I specify a console application’s exit code?


In.NET, I have a simple terminal application. It’s merely an element of a broader application that’s being tested. I’d like to specify my console application’s “exit code.” How do I go about doing this?

Asked by MrDatabase

Solution #1

Three options:

Different techniques can be utilized depending on your application (console, service, online application, etc.).

Answered by TheSoftwareJedi

Solution #2

A cry for sanity, in addition to the replies covering the return int’s. Please, please, please define your exit codes as an enum, preferably with Flags. It simplifies troubleshooting and maintenance (and, as a bonus, you can quickly print the exit codes from your help screen – you do have one, right?).

enum ExitCode : int {
  Success = 0,
  InvalidLogin = 1,
  InvalidFilename = 2,
  UnknownError = 10

int Main(string[] args) {
   return (int)ExitCode.Success;

Answered by Mark Brackett

Solution #3

You can get an exit code from a console program using one of three approaches.

The fact that 0 indicates ‘Success’ is a crucial standard to remember.

Consider utilizing an enumeration to define the exit codes that your program will return, which is a similar issue. You can use the FlagsAttribute to return a list of codes.

A ‘Console Application’ is a type of software that runs on a computer.

Answered by Scott Munro

Solution #4

If you’re going to use David’s method, be sure to check out the [Flags] Attribute as well.

Enums can now be used to do bitwise operations.

enum ExitCodes : int
  Success = 0,
  SignToolNotInPath = 1,
  AssemblyDirectoryBad = 2,
  PFXFilePathBad = 4,
  PasswordMissing = 8,
  SignFailed = 16,
  UnknownError = 32


(ExitCodes.SignFailed | ExitCodes.UnknownError)

🙂 The answer is 16 + 32.

Answered by Aron Tsang

Solution #5


See Environment.ExitCode Property.

Answered by albertein

Post is based on