Take the 2-minute tour ×

I am trying to set and get the application exit code .

I am trying to do something following :

protected override void OnStartup(StartupEventArgs e)

    if ( e.Args.Length != 0)

        new MainWindow().ShowDialog();
    Environment.ExitCode = 110;

And then I am trying in cmd to get it by echo %ERRORLEVEL%

But I get always result 0 , any idea what is the issue ?

share|improve this question
try out ERRORLEVEL without percent sign –  sll Oct 14 '11 at 18:36
@sll , this way i get result ERRORLEVEL –  Night Walker Oct 14 '11 at 18:38

6 Answers 6

up vote 8 down vote accepted

For WPF, try


Note that the application needs to be running as a console app. This answer is the easiest way I know of; the accepted answer looks more difficult.

An easy test to tell if you're running in console mode: call your app from the command line (make sure your code doesn't shut down right away). The main window should be showing. If you can type another command in the console, your app is not running in its context. The command prompt should be locked, waiting for you to close the window.

share|improve this answer
Be careful when testing this from within Visual Studio as the exit code will be 0 since it comes from the debugging host process. I didn't have to change to console app at all, my WPF win app is returning correct exit codes directly. –  Marc Nov 16 '11 at 12:19

It works for me with either method (Environment.ExitCode=110 or Environment.Exit(110)). I hope you are calling the program from the console and not from Visual Studio to then check the ExitCode...

share|improve this answer

override the OnExit method, and in the ExitEventArgs you can set that value.

 protected override void OnExit(ExitEventArgs e)
      e.ApplicationExitCode = your_value;
share|improve this answer

You can do it in Main method. Just change its return-value-type to int instead of void and return your exit-code

static int Main(string[] args) {
    // something to do
    return 110;


To create a custom Main in WPF application, you should follow these steps:

  • First: unload the project by right-click on it in Solution Explorer and click on Unload Project

  • Modify the .csproj file by change the <ApplicationDefinition Include="App.xaml"> to this one: <Page Include="App.xaml">

  • Now you can create your own Main method in your project:

Sample Main method and App class:

public partial class App : Application {

    public static int Main() {
        App app = new App();
        var i = app.Run();
        return i;

    public App() : base() { }

    protected override void OnExit(ExitEventArgs e) {
        e.ApplicationExitCode = 110;
share|improve this answer
it's WPF so i don't have main –  Night Walker Oct 14 '11 at 18:42
OK, I update my answer to WPF –  Javad_Amiry Oct 14 '11 at 20:42
@Mark I tried this and got **110**! It's might you do something wrong, and so I can't understand why did you vote the answer down?! –  Javad_Amiry Nov 16 '11 at 12:06
@Javad_Amiry: Sorry it was my error. I was testing it from within Visual Studio and thus the debugger host process was interfering with my tests... I've removed the wrong comment but can apparently only remove the down vote if you edit the question, sorry. Thanks a lot for your suggested solution. –  Marc Nov 16 '11 at 12:16

Do it like this:


This will terminate the current application with exit code 110.

share|improve this answer
I think he needs to set the exit code and then call ShutDown. If he does what you say, ShutDown won't be called. –  Icarus Oct 14 '11 at 18:39
Same problem I am getting 0 –  Night Walker Oct 14 '11 at 18:40
-1 that is not the issue. Both methods yield the same result. –  Icarus Oct 14 '11 at 18:46

You can do this in the following ways...


Shoutdown() returns a code. and Exit() also returns an exit code, but Close() only closes the application.

share|improve this answer

Your Answer


