WPF Application exit code

前端 未结 6 837
悲&欢浪女
悲&欢浪女 2020-12-17 15:39

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

I am trying to do something following :

protected override void OnStartup(StartupEventArgs e)
         


        
6条回答
  •  青春惊慌失措
    2020-12-17 16:18

    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 to this one:

    • 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);
        }
    }
    

提交回复
热议问题