16

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

I am trying to do something following :

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    if ( e.Args.Length != 0)
    {


    }
    else
    {
        new MainWindow().ShowDialog();
    }
    Environment.ExitCode = 110;
    this.Shutdown();
}

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 ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Night Walker
  • 20,638
  • 52
  • 151
  • 228

6 Answers6

16

For WPF, try

Application.Current.Shutdown(110);

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.

Community
  • 1
  • 1
default.kramer
  • 5,943
  • 2
  • 32
  • 50
  • 2
    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
  • http://www.symantec.com/connect/articles/windows-system-error-codes-exit-codes-description why is it 110? – Idan Oct 19 '15 at 07:23
  • 1
    @Idan that value came from the question. You should use whatever value is appropriate for your situation. – default.kramer Oct 19 '15 at 16:13
3

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
    Console.ReadKey();
    return 110;
}

UPDATE:

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 {

    [STAThread]
    public static int Main() {
        App app = new App();
        app.InitializeComponent();
        var i = app.Run();
        return i;
    }

    public App() : base() { }

    protected override void OnExit(ExitEventArgs e) {
        e.ApplicationExitCode = 110;
        base.OnExit(e);
    }
}
amiry jd
  • 27,021
  • 30
  • 116
  • 215
  • 1
    it's WPF so i don't have main – Night Walker Oct 14 '11 at 18: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?! – amiry jd 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
2

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

 protected override void OnExit(ExitEventArgs e)
 {
      e.ApplicationExitCode = your_value;
 }
Blau
  • 5,742
  • 1
  • 18
  • 27
2

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...

Icarus
  • 63,293
  • 14
  • 100
  • 115
0

You can do this in the following ways...

        Application.Current.Shutdown(110);
        Environment.Exit(10);
        this.Close();

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

Tapan kumar
  • 6,719
  • 1
  • 24
  • 25
0

Do it like this:

Environment.Exit(110);

This will terminate the current application with exit code 110.

qJake
  • 16,821
  • 17
  • 83
  • 135