首页 > 代码库 > C#关闭进程的2种方法
C#关闭进程的2种方法
private void KillProcess(string processName)
{
System.Diagnostics.Process myproc = new System.Diagnostics.Process();
//得到所有打开的进程
try
{
foreach (Process thisproc in Process.GetProcessesByName(processName))
{
//找到程序进程,kill之。
if (!thisproc.CloseMainWindow())
{
thisproc.Kill();
}
}
}
catch (Exception Exc)
{
MessageBox.Show(Exc.Message);
}
}
public static void StopProcess(string processName)
{
try
{
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName(processName);
foreach (System.Diagnostics.Process p in ps)
{
p.Kill();
}
}
catch (Exception ex)
{
throw ex;
}
}
C#关闭进程的2种方法