2 回答

TA貢獻1793條經驗 獲得超6個贊
if(Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)).Length > 1)
{
? ? foreach (var process in Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)))
? ? {
? ? ? ? if (process.Id != Process.GetCurrentProcess().Id)
? ? ? ? {
? ? ? ? ? ? process.Kill();
? ? ? ? }
? ? }
}
這段代碼獲取與你的同名的進程并殺死舊的進程,新的進程是殺手進程。

TA貢獻2019條經驗 獲得超9個贊
它并不干凈,但您可以使用 shell 命令:
ProcessStartInfo Info = new ProcessStartInfo();
Info.Arguments = "/C ping 127.0.0.1 -n 4 && cd \"" + Application.StartupPath + "\" && filename.exe";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
System.Timers.Timer t = new System.Timers.Timer(1000);
t.Elapsed += T_Elapsed;
t.Start();
Process.Start(Info);
private void T_Elapsed(object sender, ElapsedEventArgs e)
{
Application.Exit();
}
shell 命令 ping localhost 4 次以打發時間,在這些 ping 期間,程序退出。程序退出后,原來的 shell 命令仍在運行,因此 ping 后程序會重新打開。
- 2 回答
- 0 關注
- 188 瀏覽
添加回答
舉報