服务

  1. //一、安装服务:   
  2. private void InstallService(IDictionary stateSaver, string filepath)   
  3.         {   
  4.             try  
  5.             {   
  6.                 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController("ServiceName");   
  7.                 if(!ServiceIsExisted("ServiceName"))   
  8.                 {   
  9.                     //Install Service   
  10.                     AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();   
  11.                     myAssemblyInstaller.UseNewContext = true;   
  12.                     myAssemblyInstaller.Path =filepath;   
  13.                     myAssemblyInstaller.Install(stateSaver);   
  14.                     myAssemblyInstaller.Commit(stateSaver);   
  15.                     myAssemblyInstaller.Dispose();   
  16.                     //--Start Service   
  17.                     service.Start();   
  18.                 }   
  19.                 else  
  20.                 {   
  21.                     if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)   
  22.                     {   
  23.                         service.Start();   
  24.                     }   
  25.                 }   
  26.             }   
  27.             catch (Exception ex)   
  28.             {   
  29.                 throw new Exception("installServiceError/n" + ex.Message);   
  30.             }   
  31.         }   
  32.   
  33. //二、卸载windows服务:   
  34.         private void UnInstallService(string filepath)   
  35.         {   
  36.             try  
  37.             {   
  38.                 if (ServiceIsExisted("ServiceName"))   
  39.                 {   
  40.                     //UnInstall Service   
  41.                     AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();   
  42.                     myAssemblyInstaller.UseNewContext = true;   
  43.                     myAssemblyInstaller.Path = filepath;   
  44.                     myAssemblyInstaller.Uninstall(null);   
  45.                     myAssemblyInstaller.Dispose();   
  46.                 }   
  47.             }   
  48.             catch (Exception ex)   
  49.             {   
  50.                 throw new Exception("unInstallServiceError/n" + ex.Message);   
  51.             }   
  52.         }   
  53.   
  54. //三、判断window服务是否存在:   
  55.         private bool ServiceIsExisted(string serviceName)   
  56.         {   
  57.             ServiceController[] services = ServiceController.GetServices();   
  58.             foreach (ServiceController s in services)   
  59.             {   
  60.                 if (s.ServiceName == serviceName)   
  61.                 {   
  62.                     return true;   
  63.                 }   
  64.             }   
  65.             return false;   
  66.         }   
  67.   
  68. //四、启动服务:   
  69. private void StartService(string serviceName)   
  70.         {   
  71.             if (ServiceIsExisted(serviceName))   
  72.             {   
  73.                 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);   
  74.                 if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)   
  75.                 {   
  76.                     service.Start();   
  77.                     for (int i = 0; i < 60; i++)   
  78.                     {   
  79.                         service.Refresh();   
  80.                         System.Threading.Thread.Sleep(1000);   
  81.                         if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)   
  82.                         {   
  83.                             break;   
  84.                         }   
  85.                         if (i == 59)   
  86.                         {   
  87.                             throw new Exception(startServiceError.Replace("$s$", serviceName));   
  88.                         }   
  89.                     }   
  90.                 }   
  91.             }   
  92.         }   
  93.   
  94. //五、停止服务:   
  95.         private void StopService(string serviceName)   
  96.         {   
  97.             if (ServiceIsExisted(serviceName))   
  98.             {   
  99.                 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);   
  100.                 if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)   
  101.                 {   
  102.                     service.Stop();   
  103.                     for (int i = 0; i < 60; i++)   
  104.                     {   
  105.                         service.Refresh();   
  106.                         System.Threading.Thread.Sleep(1000);   
  107.                         if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)   
  108.                         {   
  109.                             break;   
  110.                         }   
  111.                         if (i == 59)   
  112.                         {   
  113.                             throw new Exception(stopServiceError.Replace("$s$", serviceName));   
  114.                         }   
  115.                     }   
  116.                 }   
  117.             }   
  118.         }   
  119.   
  120. // 停止指定的服务   
  121. public string StartService(string serviceName)   
  122. {   
  123. string strRst = null;   
  124. ManagementObject mo = this.managementClass.CreateInstance();   
  125. mo.Path = new ManagementPath(this.strPath+".Name=/""+serviceName+"/"");   
  126. try  
  127. {   
  128. if((string)mo["State"]=="Stopped")//!(bool)mo["AcceptStop"]   
  129. mo.InvokeMethod("StartService",null);   
  130. }   
  131. catch(ManagementException e)   
  132. {   
  133. strRst =e.Message;   
  134. }   
  135. return strRst;   
  136. }   
  137. // 暂停指定的服务   
  138. public string PauseService(string serviceName)   
  139. {   
  140. string strRst = null;   
  141. ManagementObject mo = this.managementClass.CreateInstance();   
  142. mo.Path = new ManagementPath(this.strPath+".Name=/""+serviceName+"/"");   
  143. try  
  144. {   
  145. //判断是否可以暂停   
  146. if((bool)mo["acceptPause"]&&(string)mo["State"]=="Running")   
  147. mo.InvokeMethod("PauseService",null);   
  148. }   
  149. catch(ManagementException e)   
  150. {   
  151. strRst =e.Message;   
  152. }   
  153. return strRst;   
  154. }   
  155. // 恢复指定的服务   
  156. public string ResumeService(string serviceName)   
  157. {   
  158. string strRst = null;   
  159. ManagementObject mo = this.managementClass.CreateInstance();   
  160. mo.Path = new ManagementPath(this.strPath+".Name=/""+serviceName+"/"");   
  161. try  
  162. {   
  163. //判断是否可以恢复   
  164. if((bool)mo["acceptPause"]&&(string)mo["State"]=="Paused")   
  165. mo.InvokeMethod("ResumeService",null);   
  166. }   
  167. catch(ManagementException e)   
  168. {   
  169. strRst =e.Message;   
  170. }   
  171. return strRst;   
  172. }   
  173. // 停止指定的服务   
  174. public string StopService(string serviceName)   
  175. {   
  176. string strRst = null;   
  177. ManagementObject mo = this.managementClass.CreateInstance();   
  178. mo.Path = new ManagementPath(this.strPath+".Name=/""+serviceName+"/"");   
  179. try  
  180. {   
  181. //判断是否可以停止   
  182. if((bool)mo["AcceptStop"])//(string)mo["State"]=="Running"   
  183. mo.InvokeMethod("StopService",null);   
  184. }   
  185. catch(ManagementException e)   
  186. {   
  187. strRst =e.Message;   
  188. }   
  189. return strRst;   
  190. }   
  191. }   
  192. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值