C# WindowsServer 服务的运行状态,启动,停止

做了一个Windows Form 客户端程序,其中一个功能需要监控一个服务的运行状态,如果服务是已启动,需要提供停止服务的功能,反之。

需要用到一下类文件中的方法,即可实现功能需求:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 using System.Collections;
  7 using System.Configuration.Install;
  8 using System.ServiceProcess;
  9 using Microsoft.Win32;
 10 
 11 namespace BusinessRules
 12 {
 13     public class WindowsServerControl
 14     {
 15         /// <summary>  
 16         /// 安装服务  
 17         /// </summary>  
 18         private bool InstallService(string NameService)
 19         {
 20             bool flag = true;
 21             if (!IsServiceIsExisted(NameService))
 22             {
 23                 try
 24                 {
 25                     string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
 26                     string serviceFileName = location.Substring(0, location.LastIndexOf('\\') + 1) + NameService + ".exe";
 27                     InstallmyService(null, serviceFileName);
 28                 }
 29                 catch
 30                 {
 31                     flag = false;
 32                 }
 33 
 34             }
 35             return flag;
 36         }
 37 
 38         /// <summary>  
 39         /// 卸载服务  
 40         /// </summary>  
 41         private bool UninstallService(string NameService)
 42         {
 43             bool flag = true;
 44             if (IsServiceIsExisted(NameService))
 45             {
 46                 try
 47                 {
 48                     string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
 49                     string serviceFileName = location.Substring(0, location.LastIndexOf('\\') + 1) + NameService + ".exe";
 50                     UnInstallmyService(serviceFileName);
 51                 }
 52                 catch
 53                 {
 54                     flag = false;
 55                 }
 56             }
 57             return flag;
 58         }
 59 
 60         /// <summary>  
 61         /// 检查服务存在的存在性  
 62         /// </summary>  
 63         /// <param name=" NameService ">服务名</param>  
 64         /// <returns>存在返回 true,否则返回 false;</returns>  
 65         public static bool IsServiceIsExisted(string NameService)
 66         {
 67             ServiceController[] services = ServiceController.GetServices();
 68             foreach (ServiceController s in services)
 69             {
 70                 if (s.ServiceName.ToLower() == NameService.ToLower())
 71                 {
 72                     return true;
 73                 }
 74             }
 75             return false;
 76         }
 77 
 78         /// <summary>  
 79         /// 安装Windows服务  
 80         /// </summary>  
 81         /// <param name="stateSaver">集合</param>  
 82         /// <param name="filepath">程序文件路径</param>  
 83         public static void InstallmyService(IDictionary stateSaver, string filepath)
 84         {
 85             AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
 86             AssemblyInstaller1.UseNewContext = true;
 87             AssemblyInstaller1.Path = filepath;
 88             AssemblyInstaller1.Install(stateSaver);
 89             AssemblyInstaller1.Commit(stateSaver);
 90             AssemblyInstaller1.Dispose();
 91         }
 92 
 93         /// <summary>  
 94         /// 卸载Windows服务  
 95         /// </summary>  
 96         /// <param name="filepath">程序文件路径</param>  
 97         public static void UnInstallmyService(string filepath)
 98         {
 99             AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
100             AssemblyInstaller1.UseNewContext = true;
101             AssemblyInstaller1.Path = filepath;
102             AssemblyInstaller1.Uninstall(null);
103             AssemblyInstaller1.Dispose();
104         }
105 
106         /// <summary>  
107         /// 判断某个Windows服务是否启动  
108         /// </summary>  
109         /// <returns></returns>  
110         public static bool IsServiceStart(string serviceName)
111         {
112             ServiceController psc = new ServiceController(serviceName);
113             bool bStartStatus = false;
114             try
115             {
116                 if (!psc.Status.Equals(ServiceControllerStatus.Stopped))
117                 {
118                     bStartStatus = true;
119                 }
120 
121                 return bStartStatus;
122             }
123             catch (Exception ex)
124             {
125                 throw new Exception(ex.Message);
126             }
127         }
128 
129         /// <summary>    
130         /// 修改服务的启动项 2为自动,3为手动    
131         /// </summary>    
132         /// <param name="startType"></param>    
133         /// <param name="serviceName"></param>    
134         /// <returns></returns>    
135         public static bool ChangeServiceStartType(int startType, string serviceName)
136         {
137             try
138             {
139                 RegistryKey regist = Registry.LocalMachine;
140                 RegistryKey sysReg = regist.OpenSubKey("SYSTEM");
141                 RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet");
142                 RegistryKey services = currentControlSet.OpenSubKey("Services");
143                 RegistryKey servicesName = services.OpenSubKey(serviceName, true);
144                 servicesName.SetValue("Start", startType);
145             }
146             catch (Exception ex)
147             {
148 
149                 return false;
150             }
151             return true;
152 
153 
154         }
155 
156         /// <summary>
157         /// 启动服务
158         /// </summary>
159         /// <param name="serviceName"></param>
160         /// <returns></returns>
161         public static bool StartService(string serviceName)
162         {
163             bool flag = true;
164             if (IsServiceIsExisted(serviceName))
165             {
166                 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
167                 if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
168                 {
169                     service.Start();
170                     for (int i = 0; i < 60; i++)
171                     {
172                         service.Refresh();
173                         System.Threading.Thread.Sleep(1000);
174                         if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
175                         {
176                             break;
177                         }
178                         if (i == 59)
179                         {
180                             flag = false;
181                         }
182                     }
183                 }
184             }
185             return flag;
186         }
187 
188         /// <summary>
189         /// 停止服务
190         /// </summary>
191         /// <param name="serviceName"></param>
192         /// <returns></returns>
193         public static bool StopService(string serviceName)
194         {
195             bool flag = true;
196             if (IsServiceIsExisted(serviceName))
197             {
198                 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
199                 if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
200                 {
201                     service.Stop();
202                     for (int i = 0; i < 60; i++)
203                     {
204                         service.Refresh();
205                         System.Threading.Thread.Sleep(1000);
206                         if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
207                         {
208                             break;
209                         }
210                         if (i == 59)
211                         {
212                             flag = false;
213                         }
214                     }
215                 }
216             }
217             return flag;
218         }  
219     }
220 }
WindowsServerControl
posted @ 2015-09-01 15:03  Tea_or_Coffee  阅读(532)  评论(0)    收藏  举报