.NET服务启动、停止(转载)

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;

    }
上一篇:使用Domino自带的模板定制其Web登录界面


下一篇:AcWing854 floyd求最短路