Dutton

Returning errorlevels from C# applications

Something came up today which turned out to be a bit of a blast from the past. If you want to check the return value of a C# console application (for use in an old school batch file for example) you can't rely on the return value of main (as you would in C for example), but have to set the value of System.Environment.ExitCode instead. The advantage to this over a single return statement is this call sets the exit code and won't actually exit your program at that point, see below.

static void Main(string[] args)
{
	// program code...
	// now set the return value to 1
	System.Environment.ExitCode = 1
}

This example returns 1, which can then be picked up by some good ol' fashioned batch command-goodness. I'm already reminiscing back to my 1000+ line BBS mailer batch file back in 1995!

Also came across this which provides guidance on the special meanings of some exit codes.


Share this: