C#操作服務類

來源:互聯網
上載者:User
//一、安裝服務:private void InstallService(IDictionary stateSaver, string filepath)        {            try            {                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController("ServiceName");                if(!ServiceIsExisted("ServiceName"))                {                    //Install Service                    AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();                    myAssemblyInstaller.UseNewContext = true;                    myAssemblyInstaller.Path =filepath;                    myAssemblyInstaller.Install(stateSaver);                    myAssemblyInstaller.Commit(stateSaver);                    myAssemblyInstaller.Dispose();                    //--Start Service                    service.Start();                }                else                {                    if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)                    {                        service.Start();                    }                }            }            catch (Exception ex)            {                throw new Exception("installServiceError\n" + ex.Message);            }        }//二、卸載windows服務:        private void UnInstallService(string filepath)        {            try            {                if (ServiceIsExisted("ServiceName"))                {                    //UnInstall Service                    AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();                    myAssemblyInstaller.UseNewContext = true;                    myAssemblyInstaller.Path = filepath;                    myAssemblyInstaller.Uninstall(null);                    myAssemblyInstaller.Dispose();                }            }            catch (Exception ex)            {                throw new Exception("unInstallServiceError\n" + ex.Message);            }        }//三、判斷window服務是否存在:        private bool ServiceIsExisted(string serviceName)        {            ServiceController[] services = ServiceController.GetServices();            foreach (ServiceController s in services)            {                if (s.ServiceName == serviceName)                {                    return true;                }            }            return false;        }//四、啟動服務:private void StartService(string serviceName)        {            if (ServiceIsExisted(serviceName))            {                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);                if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)                {                    service.Start();                    for (int i = 0; i < 60; i++)                    {                        service.Refresh();                        System.Threading.Thread.Sleep(1000);                        if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)                        {                            break;                        }                        if (i == 59)                        {                            throw new Exception(startServiceError.Replace("$s$", serviceName));                        }                    }                }            }        }//五、停止服務:        private void StopService(string serviceName)        {            if (ServiceIsExisted(serviceName))            {                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);                if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)                {                    service.Stop();                    for (int i = 0; i < 60; i++)                    {                        service.Refresh();                        System.Threading.Thread.Sleep(1000);                        if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)                        {                            break;                        }                        if (i == 59)                        {                            throw new Exception(stopServiceError.Replace("$s$", serviceName));                        }                    }                }            }        } // 停止指定的服務public string StartService(string serviceName){string strRst = null;ManagementObject mo = this.managementClass.CreateInstance();mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");try{if((string)mo["State"]=="Stopped")//!(bool)mo["AcceptStop"]mo.InvokeMethod("StartService",null);}catch(ManagementException e){strRst =e.Message; }return strRst;}// 暫停指定的服務public string PauseService(string serviceName){string strRst = null;ManagementObject mo = this.managementClass.CreateInstance();mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");try{//判斷是否可以暫停if((bool)mo["acceptPause"]&&(string)mo["State"]=="Running")mo.InvokeMethod("PauseService",null);}catch(ManagementException e){strRst =e.Message; }return strRst;}// 恢複指定的服務public string ResumeService(string serviceName){string strRst = null;ManagementObject mo = this.managementClass.CreateInstance();mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");try{//判斷是否可以恢複if((bool)mo["acceptPause"]&&(string)mo["State"]=="Paused")mo.InvokeMethod("ResumeService",null);}catch(ManagementException e){strRst =e.Message; }return strRst;}// 停止指定的服務public string StopService(string serviceName){string strRst = null;ManagementObject mo = this.managementClass.CreateInstance();mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");try{//判斷是否可以停止if((bool)mo["AcceptStop"])//(string)mo["State"]=="Running"mo.InvokeMethod("StopService",null);}catch(ManagementException e){strRst =e.Message; }return strRst;}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.