1.运行效果

2.代码
2.1
using System;
using System.Windows.Forms;
namespace 程序重启自己
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 重启_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
Application.Exit();
}
}
}
2.2
using System;
using System.Windows.Forms;
namespace 重启自己2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 重启_Click(object sender, EventArgs e)
{
Application.Restart();
//Environment.Exit(0);
}
}
}
2.3
using System;
using System.Windows.Forms;
namespace 程序重启自己3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 重启_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(Application.ExecutablePath); // to start new instance of application
this.Close(); //to turn off current app
//Environment.Exit(0);
//`Enviorment.Exit` 是一个极具侵入性的退出,因为它会阻止应用程序清理代码运行。大多数时候不是正确的选择。
}
}
}
本文介绍了三种使用C#实现程序自我重启的方法:通过启动进程并关闭当前实例、利用Application.Restart()方法以及结合Process.Start与Close方法。每种方法都有其适用场景。
1172

被折叠的 条评论
为什么被折叠?



