runxinzhi.com
首页
百度搜索
C#操作服务类
//一、安装服务: 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; } } }
作者:
Angelo Lee
出处:
http://www.cnblogs.com/yefengmeander/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
相关阅读:
verdi issues on license
geci
组合数据类型练习
熟悉常用的Linux操作
1.大数据概述
c语言文法分析
词法分析器#include<stdio.h> #include<string.h> #include<iostream.h> char prog[80],token[8]; char ch; int syn,p,m=0,n,row,sum=0; char *rwtab[6]={"begin","if","then","while","do","end"
关于编译原理
可变参数
函数和指针
原文地址:https://www.cnblogs.com/yefengmeander/p/2887801.html
最新文章
标签嵌套注意事项
font:设置字体样式时不常注意的点
HTML中的特殊字符
vscode中,移动端rem的使用
敏捷冲刺每日报告三(Java-Team)
敏捷冲刺每日报告二(Java-Team)
敏捷冲刺每日报告一(Java-Team)
第五次作业(284+286+288+142+143)
软件工程第四次作业(选题、进程安排)
第三次作业(结对编程)
热门文章
2017282110288软件工程第二次作业
`define marcos usage in system verilog
diary
how to set interface arrays : Illegal index into array of interfaces
grep and regular expression
Illegal assignment to type String from class mtiUvm.uvm_pkg::uvm_analysis_port #()
Clocking block output is not legal in this # or another expression.
qverilog
error : uvm_component_utils is undefined
xorg.conf disappeared in centos6
Copyright © 2020-2023
润新知