C# 代码重启windows服务

版权声明:本文为博主原创文章,博主的辛苦付出希望大家尊重,未经博主允许不得抄袭,转载请标明出处。 https://blog.csdn.net/LongtengGensSupreme/article/details/83023603

C# 代码重启windows服务

C#代码:

ServiceController service = new ServiceController("wwService");
protected void btnRestart_Click(object sender, EventArgs e)
    {
        try
        {
            if (service.Status == ServiceControllerStatus.Running)
            {
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped);
            }
            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running);

        }
        catch (Exception)
        {
        }
    }

Web.Config 配置文件:

如果配置文件不配置这个的话会报一个错误:

Win32Exception (0x80004005): Access is denied  
[InvalidOperationException: Cannot open wwService service on computer '.'.]

<system.web> 
 
<identity impersonate="true" userName="服务器登录名" password="登录密码"/> 
 
</system.web>

 原文链接:https://www.cnblogs.com/xiaopin/archive/2012/03/22/2411218.html

猜你喜欢

转载自blog.csdn.net/LongtengGensSupreme/article/details/83023603
今日推荐