1. 程式人生 > >C# 程式碼重啟windows服務

C# 程式碼重啟windows服務

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