Possible Duplicate:
Capture console exit C#
When I run my console app and the user closes it by closing the console window, the exit code is 0. How to set specific error code in such cases?
Possible Duplicate:
Capture console exit C#
When I run my console app and the user closes it by closing the console window, the exit code is 0. How to set specific error code in such cases?
One way is to set the ExitCode to whatever you want at anytime throughout the program and at the end of the program, set it to 0. In my example, if the user closes the console before the application completes, it reports ExitCode -1, otherwise it sets the ExitCode to 0:
static void Main(string[] args)
{
Environment.ExitCode = -1;
for (int i = 0; i < 20; i++)
{
Thread.Sleep(1000);
}
Environment.ExitCode = 0;
Console.WriteLine("Done!");
}