• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#操作服务类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
//一、安装服务: 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; } } }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# -- 偶遇 模除发布时间:2022-07-10
下一篇:
跟我学做c#皮肤美化(七)----窗体换肤发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap