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

C#.NetExit Code

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

Three 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 application, etc.), different methods can be used.

Solution 2 - C#

In addition to the answers covering the return int's... a plea for sanity. Please, please define your exit codes in an enum, with Flags if appropriate. It makes debugging and maintenance so much easier (and, as a bonus, you can easily print out the exit codes on your help screen - you do have one of those, right?).

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

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

Solution 3 - C#

There are three methods that you can use to return an exit code from a console application.

  1. Modify the Main method in your application so that it returns an int instead of void (a function that returns an Integer instead of Sub in VB.NET) and then return the exit code from that method.
  2. Set the Environment.ExitCode property to the exit code. Note that method 1. takes precedence - if the Main method returns anything other than void (is a Sub in VB.Net) then the value of this property will be ignored.
  3. Pass the exit code to the Environment.Exit method. This will terminate the process immediately as opposed to the other two methods.

An important standard that should be observed is that 0 represents 'Success'.

On a related topic, consider using an enumeration to define the exit codes that your application is going to return. The FlagsAttribute will allow you to return a combination of codes.

Also, ensure that your application is compiled as a 'Console Application'.

Solution 4 - C#

If you are going to use the method suggested by David, you should also take a look at the [Flags] Attribute.

This allows you to do bit wise operations on enums.

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

Then

(ExitCodes.SignFailed | ExitCodes.UnknownError)

would be 16 + 32. :)

Solution 5 - C#

System.Environment.ExitCode 

See Environment.ExitCode Property.

Solution 6 - C#

int code = 2;
Environment.Exit( code );

Solution 7 - C#

Just return the appropiate code from main.

int Main(string[] args)
{
    return 0; // Or exit code of your choice
}

Solution 8 - C#

Use ExitCode if your main has a void return signature. Otherwise, you need to "set" it by the value you return.

From Environment.ExitCode Property:

> If the Main method returns void, you can use this property to set the exit code that will be returned to the calling environment. If Main does not return void, this property is ignored. The initial value of this property is zero.

Solution 9 - C#

The enumeration option is excellent. However, it can be improved upon by multiplying the numbers as in:

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

In the case of multiple errors, adding the specific error numbers together will give you a unique number that will represent the combination of detected errors.

For example, an errorlevel of 6 can only consist of errors 4 and 2, 12 can only consist of errors 4 and 8, 14 can only consist of 2, 4 and 8 etc.

Solution 10 - C#

As an update to Scott Munro's answer:

Solution 11 - C#

You can find the system error codes on System Error Codes (0-499).

You will find the typical codes, like 2 for "file not found" or 5 for "access denied".

And when you stumble upon an unknown code, you can use this command to find out what it means:

net helpmsg decimal_code

For example,

net helpmsg 1

returns

Incorrect function

Solution 12 - C#

Use this code

Environment.Exit(0);

use 0 as the int if you don't want to return anything.

Solution 13 - C#

I'm doing it like this:

int exitCode = 0;
Environment.Exit(exitCode);

Or you can throw an error (personal preference):

throw new ArgumentException("Code 0, Environment Exit");

I've choose ArgumentException, but you can type other. It will work fine.

Solution 14 - C#

Just another way:

public static class ApplicationExitCodes
{
	public static readonly int Failure = 1;
	public static readonly int Success = 0;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMrDatabaseView Question on Stackoverflow
Solution 1 - C#TheSoftwareJediView Answer on Stackoverflow
Solution 2 - C#Mark BrackettView Answer on Stackoverflow
Solution 3 - C#Scott MunroView Answer on Stackoverflow
Solution 4 - C#Aron TsangView Answer on Stackoverflow
Solution 5 - C#alberteinView Answer on Stackoverflow
Solution 6 - C#palehorseView Answer on Stackoverflow
Solution 7 - C#Esteban ArayaView Answer on Stackoverflow
Solution 8 - C#crashmstrView Answer on Stackoverflow
Solution 9 - C#DavidView Answer on Stackoverflow
Solution 10 - C#Vern DeHavenView Answer on Stackoverflow
Solution 11 - C#Fred MauroyView Answer on Stackoverflow
Solution 12 - C#Swastik BhattacharyyaView Answer on Stackoverflow
Solution 13 - C#Victor PetrovView Answer on Stackoverflow
Solution 14 - C#isxakerView Answer on Stackoverflow