博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 服务的安装、卸载、启动、停止操作
阅读量:5322 次
发布时间:2019-06-14

本文共 5120 字,大约阅读时间需要 17 分钟。

  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;
            }

转载于:https://www.cnblogs.com/kenchan/archive/2011/07/16/2108304.html

你可能感兴趣的文章
微软职位内部推荐-Sr. SE - Office incubation
查看>>
微软职位内部推荐-SOFTWARE ENGINEER II
查看>>
centos系统python2.7更新到3.5
查看>>
C#类与结构体究竟谁快——各种函数调用模式速度评测
查看>>
我到底要选择一种什么样的生活方式,度过这一辈子呢:人生自由与职业发展方向(下)...
查看>>
poj 题目分类
查看>>
windows 安装yaml支持和pytest支持等
查看>>
读书笔记:季羡林关于如何做研究学问的心得
查看>>
面向对象的优点
查看>>
套接口和I/O通信
查看>>
阿里巴巴面试之利用两个int值实现读写锁
查看>>
浅谈性能测试
查看>>
Winform 菜单和工具栏控件
查看>>
jequery动态创建form
查看>>
CDH版本大数据集群下搭建的Hue详细启动步骤(图文详解)
查看>>
第六次java作业
查看>>
巧用Win+R
查看>>
浅析原生js模仿addclass和removeclass
查看>>
Python中的greenlet包实现并发编程的入门教程
查看>>
java中遍历属性字段及值(常见方法)
查看>>