1 using System;
  2 using System.Collections.Generic;
  3 using System.Collections;
  4 using System.Linq;
  5 using System.Text;
  6 
  7 using System.ServiceProcess;
  8 using System.Configuration.Install;
  9 
 10 namespace WindowsFormsApplication2
 11 {
 12     public class ServiceManage
 13     {
 14         public ServiceManage()
 15         {
 16         }
 17 
 18         private string _serviceName;
 19 
 20         public string serviceName
 21         {
 22             get { return _serviceName; }
 23             set { _serviceName = value; }
 24         }
 25 
 26         private int _overflag = -1;
 27 
 28         public int overflag
 29         {
 30             get { return _overflag; }
 31             set { _overflag = value; }
 32         }
 33 
 34 
 35         /// <summary>         
 36         /// 枚举系统服务         
 37         /// </summary>        
 38         private void EnumServices()
 39         {
 40             ServiceController[] svcs = ServiceController.GetServices();
 41             foreach (ServiceController svc in svcs)
 42             {
 43             }
 44         }
 45 
 46 
 47         public void InstallService()
 48         {
 49             _overflag = 0;
 50             IDictionary stateSaver = new Hashtable();
 51             string _AssemblyFileName = string.Concat(AppDomain.CurrentDomain.BaseDirectory, "ken.WinService.exe");
 52 
 53             try
 54             {
 55                 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(_serviceName);
 56 
 57                 if (!ServiceIsExisted())
 58                 {
 59                     //Install Service
 60                     AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
 61 
 62                     myAssemblyInstaller.UseNewContext = true;
 63 
 64                     myAssemblyInstaller.Path = _AssemblyFileName;
 65                     
 66                     myAssemblyInstaller.Install(stateSaver);
 67 
 68                     myAssemblyInstaller.Commit(stateSaver);
 69 
 70                     myAssemblyInstaller.Dispose();
 71 
 72                     //--Start Service
 73 
 74                     service.Start();
 75 
 76                 }
 77                 else
 78                 {
 79                     if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
 80                     {
 81                         service.Start();
 82                     }
 83                 }
 84             }
 85             catch (Exception ex)
 86             {
 87                 throw new Exception("installServiceError\n" + ex.Message);
 88             }
 89             finally
 90             {
 91                 _overflag = 1;
 92             }
 93         }
 94 
 95         public void UnInstallService()
 96         {
 97             _overflag = 0;
 98             string _AssemblyFileName = string.Concat(AppDomain.CurrentDomain.BaseDirectory, "ken.WinService.exe");
 99 
100             try
101             {
102                 if (ServiceIsExisted())
103                 {
104                     //UnInstall Service
105                     AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
106 
107                     myAssemblyInstaller.UseNewContext = true;
108 
109                     myAssemblyInstaller.Path = _AssemblyFileName;
110 
111                     myAssemblyInstaller.Uninstall(null);
112 
113                     myAssemblyInstaller.Dispose();
114                 }
115             }
116             catch (Exception ex)
117             {
118                 throw new Exception("unInstallServiceError\n" + ex.Message);
119             }
120             finally
121             {
122                 _overflag = 1;
123             }
124         }
125 
126         public void StartService()
127         {
128             _overflag = 0;
129             try
130             {
131                 if (ServiceIsExisted())
132                 {
133                     System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(_serviceName);
134 
135                     if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
136                     {
137                         service.Start();
138 
139                         for (int i = 0; i < 60; i++)
140                         {
141                             service.Refresh();
142                             System.Threading.Thread.Sleep(1000);
143 
144                             if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
145                             {
146                                 break;
147                             }
148 
149                             if (i == 59)
150                             {
151                                 throw new Exception(_serviceName + " 启动失败");
152                             }
153                         }
154                     }
155                 }
156             }
157             catch
158             { }
159             finally
160             {
161                 _overflag = 1;
162             }
163         }
164 
165         public void ReStartService()
166         {
167             StopService();
168             StartService();
169         }
170 
171         public void StopService()
172         {
173             _overflag = 0;
174             try
175             {
176                 if (ServiceIsExisted())
177                 {
178                     System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(_serviceName);
179                     if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running && service.CanStop)
180                     {
181                         service.Stop();
182                         for (int i = 0; i < 60; i++)
183                         {
184                             service.Refresh();
185                             System.Threading.Thread.Sleep(1000);
186 
187                             if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
188                             {
189                                 break;
190                             }
191 
192                             if (i == 59)
193                             {
194                                 throw new Exception(_serviceName + " 停止失败");
195                             }
196                         }
197                     }
198                 }
199             }
200             catch
201             { }
202             finally
203             {
204                 _overflag = 1;
205             }
206         }
207 
208         public bool ServiceIsExisted()
209         {
210             ServiceController[] services = ServiceController.GetServices();
211 
212             foreach (ServiceController s in services)
213             {
214                 if (s.ServiceName == _serviceName)
215                 {
216                     return true;
217                 }
218             }
219 
220             return false;
221         }
222 
223         public ServiceControllerStatus getServiceState()
224         {
225             System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(_serviceName);
226 
227             return service.Status;
228         }
229     }
230 }
 
 
调用:
            ServiceManage __serviceManage = new ServiceManage();
            __serviceManage.serviceName = “servicename”;

            __serviceManage.overflag = -1;
            System.Threading.Thread _t = new System.Threading.Thread(new System.Threading.ThreadStart(__serviceManage.InstallService));
            _t.Start();

 

            while (__serviceManage.overflag < 1)
            {
                //do something;
            }

posted on 2011-07-16 17:49  ken Chan  阅读(2144)  评论(0编辑  收藏  举报