C#操作服务类
来源:互联网 发布:海外游戏数据分析师 编辑:程序博客网 时间:2024/04/27 23:58
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
}
- C#操作服务类
- C#操作服务类
- C#服务操作类 卸载 查询 停止
- C#操作windows服务
- C#操作服务
- C#安装服务和卸载Windows服务 操作类
- C#操作注册服务卸载服务启动服务停止服务..
- C#操作注册服务卸载服务启动服务停止服务.. .
- C#对Windows服务操作(注册安装服务,卸载服务,启动停止服务,判断服务存在)
- c#中对系统服务的操作
- c# Windows 服务创建与操作
- 通过C#操作SQLServer服务管理器
- 使用C#系统服务定时执行操作
- C# 创建Windows服务。服务功能:定时操作数据库
- C# 创建Windows服务。服务功能:定时操作数据库
- C# 创建Windows服务。服务功能:定时操作数据库
- C# 创建Windows服务。服务功能:定时操作数据库
- C# 创建Windows服务。服务功能:定时操作数据库
- 迷宫算法
- 一个jdbc操作类
- Android提高十八篇之自定义Menu(TabMenu)
- Paint
- 使用ZedGraph制作动态更新的统计图
- C#操作服务类
- graphic Xfermode
- graphic 透明度的bitmap
- Don't work for your salary!
- apache端口转发
- ACE_Message_Block的使用注意事项
- SQL提高查询效益之in、not in、between、like等条件讲述 数据库Sql,VFP,Access
- OpenGL ES 教程-初稿-00
- 如何利用cron进程来自动执行(总结)