Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
369 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...