public string StartService(string serviceName, bool serviceFlag)
{
try
{
using (System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(serviceName))
{
TimeSpan timeout = new TimeSpan(0, 0, 15);
//开
if (serviceFlag)
{
if (sc.Status != ServiceControllerStatus.Running)
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
}
else
{
if (sc.Status != ServiceControllerStatus.Stopped)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
}
}
}
catch (Exception e)
{
return e.Message;
}
var msg="";
if (serviceFlag)
{ msg = "开启 " + serviceName + " 服务成功"; }
else
{
msg = "关闭 " + serviceName + " 服务成功";
}
return msg;
}