Process myProcess = new Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.Start();
// 終止進程
myProcess.Kill();
Process myProcess = new Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.Start();
// 終止進程
myProcess.CloseMainWindow();
Process myProcess = new Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.Start();
// 等待進程執行完成
myProcess.WaitForExit();
[DllImport("kernel32.dll")]
public static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode);
Process myProcess = new Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.Start();
// 獲取進程句柄,并強制結束進程
TerminateProcess(myProcess.Handle, 0);
需要注意的是,強制結束一個進程可能會導致數據丟失或者系統不穩定,因此建議謹慎使用。