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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM