C# 开启及停止进程
C# 开启及停止进程
1. 首先在程序中引用: System.Diagnostics;
2. 开启进程:
/// <summary>
/// 开启进程
/// </summary>
/// <param name="aProPath">进程路径(如:@"C:\WINDOWS\system32\osk.exe")</param>
/// <returns>开启成功(true)或失败(false)</returns>
public bool StartProcess(string aProPath)
{
try
{
Process ps = Process.Start(aProPath);//开启进程
}
catch (Exception)
{
return false; //失败
}
return true; //成功
}
3. 停止进程:
/// <summary>
/// 关闭进程
/// </summary>
/// <param name="aName">进程名称(不带.exe)</param>
/// <returns>关闭成功(true)或失败(false)</returns>
public bool CloseProcess(string aName)
{
try
{
Process[] ps = Process.GetProcessesByName(aName);//根据进程名称,获取该进程信息
foreach (Process p in ps)
{
p.Kill();
p.WaitForExit();
p.Close(); //关闭
}
}
catch (Exception ex)
{
return false; //失败
}
return true; //成功
}