Restart application using C#

后端 未结 4 1322
慢半拍i
慢半拍i 2020-11-30 10:59

How can I restart my WPF application using C#?

相关标签:
4条回答
  • 2020-11-30 11:28

    You can use the Windows API Code Pack's Restart and Recovery API. Just be aware that this is a new API, so it will only work on current operating systems (ie: Windows 7).

    0 讨论(0)
  • 2020-11-30 11:32

    I don't think there's a direct method in WPF like there is in WinForms. However, you could use methods from the Windowns.Form namespace like this: (You might need to add a reference to the System.Windows.Form assembly)

    System.Windows.Forms.Application.Restart();
    
    System.Windows.Application.Current.Shutdown();
    
    0 讨论(0)
  • 2020-11-30 11:33

    The code

    Process.Start(Application.ResourceAssembly.Location);
    

    does not work for .NET Core 3.1 applications. Instead use

    using System.Diagnostics;
    using System.Windows;
    
    ...
    
    Process.Start(Process.GetCurrentProcess().MainModule.FileName);
    Application.Current.Shutdown();
    

    This works also for .NET Framework applications.

    0 讨论(0)
  • 2020-11-30 11:47

    The following is the best solution I found, you don't need to add a reference to System.Windows.Forms, instead you need add the namespace System.Diagnostics which you already has a reference to its assembly:

    Process.Start(Application.ResourceAssembly.Location);
    Application.Current.Shutdown();
    
    0 讨论(0)
提交回复
热议问题