简体   繁体   English

使用 C# 重新启动应用程序

[英]Restart application using C#

如何使用 C# 重新启动 WPF 应用程序?

I don't think there's a direct method in WPF like there is in WinForms.我认为 WPF 中没有像 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)但是,您可以使用Windowns.Form命名空间中的方法,如下所示:(您可能需要添加对System.Windows.Form程序集的引用)

System.Windows.Forms.Application.Restart();

System.Windows.Application.Current.Shutdown();

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:以下是我找到的最佳解决方案,您不需要添加对 System.Windows.Forms 的引用,而是需要添加命名空间System.Diagnostics ,您已经拥有对其程序集的引用:

Process.Start(Application.ResourceAssembly.Location);
Application.Current.Shutdown();

The code代码

Process.Start(Application.ResourceAssembly.Location);

does not work for .NET Core 3.1 applications.不适用于 .NET Core 3.1 应用程序。 Instead use而是使用

using System.Diagnostics;
using System.Windows;

...

Process.Start(Process.GetCurrentProcess().MainModule.FileName);
Application.Current.Shutdown();

This works also for .NET Framework applications.这也适用于 .NET Framework 应用程序。

You can use the Windows API Code Pack 's Restart and Recovery API.您可以使用Windows API 代码包的重启和恢复 API。 Just be aware that this is a new API, so it will only work on current operating systems (ie: Windows 7).请注意,这是一个新的 API,因此它仅适用于当前的操作系统(即:Windows 7)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM