001 | //一、安装服务: |
002 | private void InstallService(IDictionary stateSaver, string filepath) |
003 | { |
004 | try |
005 | { |
006 | System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController( "ServiceName" ); |
007 | if (!ServiceIsExisted( "ServiceName" )) |
008 | { |
009 | //Install Service |
010 | AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); |
011 | myAssemblyInstaller.UseNewContext = true ; |
012 | myAssemblyInstaller.Path =filepath; |
013 | myAssemblyInstaller.Install(stateSaver); |
014 | myAssemblyInstaller.Commit(stateSaver); |
015 | myAssemblyInstaller.Dispose(); |
016 | //--Start Service |
017 | service.Start(); |
018 | } |
019 | else |
020 | { |
021 | if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending) |
022 | { |
023 | service.Start(); |
024 | } |
025 | } |
026 | } |
027 | catch (Exception ex) |
028 | { |
029 | throw new Exception( "installServiceError/n" + ex.Message); |
030 | } |
031 | } |
032 |
033 | //二、卸载windows服务: |
034 | private void UnInstallService( string filepath) |
035 | { |
036 | try |
037 | { |
038 | if (ServiceIsExisted( "ServiceName" )) |
039 | { |
040 | //UnInstall Service |
041 | AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); |
042 | myAssemblyInstaller.UseNewContext = true ; |
043 | myAssemblyInstaller.Path = filepath; |
044 | myAssemblyInstaller.Uninstall( null ); |
045 | myAssemblyInstaller.Dispose(); |
046 | } |
047 | } |
048 | catch (Exception ex) |
049 | { |
050 | throw new Exception( "unInstallServiceError/n" + ex.Message); |
051 | } |
052 | } |
053 |
054 | //三、判断window服务是否存在: |
055 | private bool ServiceIsExisted( string serviceName) |
056 | { |
057 | ServiceController[] services = ServiceController.GetServices(); |
058 | foreach (ServiceController s in services) |
059 | { |
060 | if (s.ServiceName == serviceName) |
061 | { |
062 | return true ; |
063 | } |
064 | } |
065 | return false ; |
066 | } |
067 |
068 | //四、启动服务: |
069 | private void StartService( string serviceName) |
070 | { |
071 | if (ServiceIsExisted(serviceName)) |
072 | { |
073 | System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName); |
074 | if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending) |
075 | { |
076 | service.Start(); |
077 | for ( int i = 0; i < 60; i++) |
078 | { |
079 | service.Refresh(); |
080 | System.Threading.Thread.Sleep(1000); |
081 | if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running) |
082 | { |
083 | break ; |
084 | } |
085 | if (i == 59) |
086 | { |
087 | throw new Exception(startServiceError.Replace( "$s$" , serviceName)); |
088 | } |
089 | } |
090 | } |
091 | } |
092 | } |
093 |
094 | //五、停止服务: |
095 | private void StopService( string serviceName) |
096 | { |
097 | if (ServiceIsExisted(serviceName)) |
098 | { |
099 | 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 | } |