ServiceController 类详细介绍
//如果此服务存在
if (checkService())
{
ServiceController sc = new ServiceController("mysql");
//mysql服务是否已启动,是,则停止
if (sc.CanStop && sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
}
}
if (checkService())
{
ServiceController sc = new ServiceController("mysql");
//mysql服务是否已启动,是,则停止
if (sc.CanStop && sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
}
}
/// <summary>
/// 判断服务是否存在
/// </summary>
/// <returns></returns>
private bool checkService()
{
bool result = false;
RegistryKey rk = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\mysql", false);
if (rk != null)
{
result = true;
}
return result;
}
/// 判断服务是否存在
/// </summary>
/// <returns></returns>
private bool checkService()
{
bool result = false;
RegistryKey rk = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\mysql", false);
if (rk != null)
{
result = true;
}
return result;
}