C# 重启程序的方法

  • Post author:
  • Post category:其他


C# 中一般重启exe程序有以下三种方法:

方法一:使用Restart()方法, Restart()方法需要引用System.Windows.Forms,并在Application类中,而Application.Current.Shutdown();则需要引用System.Windows。

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


**************************************************************************************************************

方法二:使用System.Diagnostics.Process.Start()

System.Reflection.Assembly.GetEntryAssembly();
string startpath = System.IO.Directory.GetCurrentDirectory();
System.Diagnostics.Process.Start(startpath + “\\xxx.exe”);
Application.Current.Shutdown();


**************************************************************************************************************

方法三:使用Process方式

Process p = new Process();
p.StartInfo.FileName = System.AppDomain.CurrentDomain.BaseDirectory + “xxx.exe”;
p.StartInfo.UseShellExecute = false;
p.Start();
Application.Current.Shutdown();


**************************************************************************************************************

注意:首先System.Windows.Forms中的Application是没有Current的;而Current是在System.Windows中的;如果你的WPF项目中没有引用System.Windows.Forms,则直接使用Application.Current.Shutdown();即可,因为新建WPF程序时,默认的就会把System.Windows添加到引用项中;如果你引用中同时包含System.Windows和System.Windows.Forms程序集,需要使用指定的命名空间或者作用域System.Windows.Application.Current.Shutdown();


**************************************************************************************************************



版权声明:本文为BYH371256原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。