最近,做了一个定时监控WINDOWS服务的程序,要做到随机启动,定时监控指定的服务,如果没有启动则开启,现在正在测试中,现在将代码及思路公布
一、思路
本程序的核心在于随机启动和WINDOWS服务上,对于随机启动,引入Microsoft.Win32命名空间,利用RegistryKey类即可完成对注册表的增、删、改等操作;对于WINDOWS服务,引入System.ServiceProcess命名空间,利用ServiceController类即可完成对系统服务的启动、停止、查询等操作。
二、
代码如下,这里程序默认为开启MSSQLSERVER服务,并添加了托盘区图标,可以在启动时或启动后最小化到托盘区:
1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.windows.Forms;
6
using System.Data;
7
using System.ServiceProcess;
8
using System.IO;
9
using Microsoft.Win32;
10
namespace WatchService
11

{
12
/**//// <summary>
13
/// Form1 的摘要说明。
14
/// </summary>
15
public class WatchService : System.windows.Forms.Form
16

{
17
private System.windows.Forms.Button btn_startWatch;
18
private System.windows.Forms.Button btn_stopWatch;
19
private System.windows.Forms.Button btn_startReg;
20
private System.windows.Forms.Button btn_stopReg;
21
private System.windows.Forms.Label lbl_appStatus;
22
private System.windows.Forms.TextBox tbx_serviceName;
23
private System.windows.Forms.Button btn_Exit;
24
private System.windows.Forms.TextBox tbx_interval;
25
private System.windows.Forms.Timer timer1;
26
private System.windows.Forms.NotifyIcon notifyIcon1;
27
private System.ComponentModel.IContainer components;
28
public WatchService()
29

{
30
//
31
// windows 窗体设计器支持所必需的
32
//
33
InitializeComponent();
34
//
35
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
36
//
37
}
38
/**//// <summary>
39
/// 清理所有正在使用的资源。
40
/// </summary>
41
protected override void Dispose( bool disposing )
42

{
43
if( disposing )
44

{
45
if (components != null)
46
{
47
components.Dispose();
48
}
49
}
50
base.Dispose( disposing );
51
}
52
windows 窗体设计器生成的代码#region windows 窗体设计器生成的代码
53
/**//// <summary>
54
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
55
/// 此方法的内容。
56
/// </summary>
57
private void InitializeComponent()
58

{
59
this.components = new System.ComponentModel.Container();
60
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(WatchService));
61
this.btn_startWatch = new System.windows.Forms.Button();
62
this.btn_stopWatch = new System.windows.Forms.Button();
63
this.btn_startReg = new System.windows.Forms.Button();
64
this.btn_stopReg = new System.windows.Forms.Button();
65
this.lbl_appStatus = new System.windows.Forms.Label();
66
this.tbx_serviceName = new System.windows.Forms.TextBox();
67
this.btn_Exit = new System.windows.Forms.Button();
68
this.tbx_interval = new System.windows.Forms.TextBox();
69
this.timer1 = new System.windows.Forms.Timer(this.components);
70
this.notifyIcon1 = new System.windows.Forms.NotifyIcon(this.components);
71
this.SuspendLayout();
72
//
73
// btn_startWatch
74
//
75
this.btn_startWatch.Location = new System.Drawing.Point(112, 8);
76
this.btn_startWatch.Name = "btn_startWatch";
77
this.btn_startWatch.Size = new System.Drawing.Size(64, 23);
78
this.btn_startWatch.TabIndex = 0;
79
this.btn_startWatch.Text = "开始监控";
80
this.btn_startWatch.Click += new System.EventHandler(this.btn_startWatch_Click);
81
//
82
// btn_stopWatch
83
//
84
this.btn_stopWatch.Location = new System.Drawing.Point(184, 8);
85
this.btn_stopWatch.Name = "btn_stopWatch";
86
this.btn_stopWatch.Size = new System.Drawing.Size(64, 23);
87
this.btn_stopWatch.TabIndex = 1;
88
this.btn_stopWatch.Text = "停止监控";
89
//
90
// btn_startReg
91
//
92
this.btn_startReg.Location = new System.Drawing.Point(112, 40);
93
this.btn_startReg.Name = "btn_startReg";
94
this.btn_startReg.Size = new System.Drawing.Size(88, 24);
95
this.btn_startReg.TabIndex = 2;
96
this.btn_startReg.Text = "开启随机启动";
97
this.btn_startReg.Click += new System.EventHandler(this.btn_startReg_Click);
98
//
99
// btn_stopReg
100
//
101
this.btn_stopReg.Location = new System.Drawing.Point(232, 40);
102
this.btn_stopReg.Name = "btn_stopReg";
103
this.btn_stopReg.Size = new System.Drawing.Size(88, 24);
104
this.btn_stopReg.TabIndex = 3;
105
this.btn_stopReg.Text = "关闭随机启动";
106
this.btn_stopReg.Click += new System.EventHandler(this.btn_stopReg_Click);
107
//
108
// lbl_appStatus
109
//
110
this.lbl_appStatus.ForeColor = System.Drawing.Color.Red;
111
this.lbl_appStatus.Location = new System.Drawing.Point(16, 72);
112
this.lbl_appStatus.Name = "lbl_appStatus";
113
this.lbl_appStatus.Size = new System.Drawing.Size(304, 23);
114
this.lbl_appStatus.TabIndex = 4;
115
this.lbl_appStatus.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
116
//
117
// tbx_serviceName
118
//
119
this.tbx_serviceName.BorderStyle = System.windows.Forms.BorderStyle.FixedSingle;
120
this.tbx_serviceName.ForeColor = System.Drawing.Color.Red;
121
this.tbx_serviceName.Location = new System.Drawing.Point(8, 8);
122
this.tbx_serviceName.Name = "tbx_serviceName";
123
this.tbx_serviceName.TabIndex = 5;
124
this.tbx_serviceName.Text = "输入服务名";
125
this.tbx_serviceName.MouseDown += new System.windows.Forms.MouseEventHandler(this.tbx_serviceName_MouseDown);
126
//
127
// btn_Exit
128
//
129
this.btn_Exit.Location = new System.Drawing.Point(256, 8);
130
this.btn_Exit.Name = "btn_Exit";
131
this.btn_Exit.Size = new System.Drawing.Size(64, 23);
132
this.btn_Exit.TabIndex = 6;
133
this.btn_Exit.Text = "退出程序";
134
this.btn_Exit.Click += new System.EventHandler(this.btn_Exit_Click);
135
//
136
// tbx_interval
137
//
138
this.tbx_interval.BorderStyle = System.windows.Forms.BorderStyle.FixedSingle;
139
this.tbx_interval.ForeColor = System.Drawing.Color.Red;
140
this.tbx_interval.Location = new System.Drawing.Point(8, 40);
141
this.tbx_interval.Name = "tbx_interval";
142
this.tbx_interval.TabIndex = 7;
143
this.tbx_interval.Text = "输入监控间隔(秒)";
144
this.tbx_interval.MouseDown += new System.windows.Forms.MouseEventHandler(this.tbx_interval_MouseDown);
145
//
146
// timer1
147
//
148
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
149
//
150
// notifyIcon1
151
//
152
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
153
this.notifyIcon1.Text = "双击打开WatchService";
154
this.notifyIcon1.Visible = true;
155
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
156
//
157
// WatchService
158
//
159
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
160
this.ClientSize = new System.Drawing.Size(328, 102);
161
this.Controls.Add(this.tbx_interval);
162
this.Controls.Add(this.btn_Exit);
163
this.Controls.Add(this.tbx_serviceName);
164
this.Controls.Add(this.lbl_appStatus);
165
this.Controls.Add(this.btn_stopReg);
166
this.Controls.Add(this.btn_startReg);
167
this.Controls.Add(this.btn_stopWatch);
168
this.Controls.Add(this.btn_startWatch);
169
this.MaximizeBox = false;
170
this.Name = "WatchService";
171
this.ShowInTaskbar = false;
172
this.Text = "WatchService";
173
this.windowstate = System.windows.Forms.Formwindowstate.Minimized;
174
this.Resize += new System.EventHandler(this.WatchService_Resize);
175
this.Load += new System.EventHandler(this.WatchService_Load);
176
this.ResumeLayout(false);
177
}
178
#endregion
179
private ServiceController scDBService;
180
private string serviceName="MSSqlServer"; // 默认服务名
181
private int iInterval=60; // 默认监控间隔(秒)
182
/**//// <summary>
183
/// 应用程序的主入口点。
184
/// </summary>
185
[STAThread]
186
static void Main()
187

{
188
Application.Run(new WatchService());
189
}
190
// 开启监控
191
private void btn_startWatch_Click(object sender, System.EventArgs e)
192

{
193
if(this.tbx_serviceName.Text=="")
194

{
195
this.lbl_appStatus.Text="服务名不能为空";
196
this.tbx_serviceName.Focus();
197
return;
198
}
199
if(this.tbx_interval.Text=="")
200

{
201
this.lbl_appStatus.Text="服务监控间隔不能为空";
202
this.tbx_interval.Focus();
203
return;
204
}
205
serviceName=this.tbx_serviceName.Text;
206
iInterval=int.Parse(this.tbx_interval.Text);
207
this.timer1.Interval=iInterval*1000;
208
startService();
209
}
210
// 开启随机启动
211
private void btn_startReg_Click(object sender, System.EventArgs e)
212

{
213
try
214

{
215
string dir=Directory.GetCurrentDirectory();
216
dir+="\\WatchService.exe";
217
RegistryKey akey=Registry.LocalMachine;
218
akey=akey.OpenSubKey(@"SOFTWARE\Microsoft\windows\CurrentVersion\Run",true);
219
akey.SetValue("WatchService",dir);
220
akey.Close();
221
this.lbl_appStatus.Text="开启随机启动成功。";
222
}
223
catch(Exception exp)
224

{
225
this.lbl_appStatus.Text="开启随机启动失败,原因:"+exp.Message;
226
}
227
}
228
private void tbx_serviceName_MouseDown(object sender, System.windows.Forms.MouseEventArgs e)
229

{
230
this.tbx_serviceName.Text="";
231
}
232
// 关闭随机启动
233
private void btn_stopReg_Click(object sender, System.EventArgs e)
234

{
235
try
236

{
237
RegistryKey akey=Registry.LocalMachine;
238
akey=akey.OpenSubKey(@"SOFTWARE\Microsoft\windows\CurrentVersion\Run",true);
239
akey.SetValue("WatchService",false);
240
akey.Close();
241
this.lbl_appStatus.Text="关闭随机启动成功。";
242
}
243
catch(Exception exp)
244

{
245
this.lbl_appStatus.Text="关闭随机启动失败,原因:"+exp.Message;
246
}
247
}
248
private void btn_Exit_Click(object sender, System.EventArgs e)
249

{
250
Application.Exit();
251
}
252
/**//// <summary>
253
/// 开启指定的windows服务
254
/// </summary>
255
private void startService()
256

{
257
try
258

{
259
scDBService=new ServiceController(serviceName);
260
ServiceController[] scAllService=ServiceController.GetServices();
261
int i=0;
262
while(i<scAllService.Length)
263
{
264
if(scAllService[i].DisplayName==serviceName)
265
{
266
if(scDBService.Status.Equals(ServiceControllerStatus.Stopped))
267
{
268
this.lbl_appStatus.Text=serviceName+"服务正在启动……";
269
scDBService.Start();
270
}
271
else if(scDBService.Status.Equals(ServiceControllerStatus.Running))
272
{
273
this.lbl_appStatus.Text=serviceName+"服务正在运行……";
274
}
275
if(!this.timer1.Enabled) this.timer1.Start();
276
return;
277
}
278
else
279
{
280
i++;
281
}
282
}
283
this.lbl_appStatus.Text=serviceName+"服务并没有安装在本机上,请检查。";
284
if(this.timer1.Enabled) this.timer1.Stop();
285
}
286
catch(Exception exp)
287

{
288
this.lbl_appStatus.Text=serviceName+"服务启动失败,原因:"+exp.Message;
289
}
290
}
291
// 监控时钟
292
private void timer1_Tick(object sender, System.EventArgs e)
293

{
294
startService();
295
}
296
private void tbx_interval_MouseDown(object sender, System.windows.Forms.MouseEventArgs e)
297

{
298
this.tbx_interval.Text="";
299
}
300
// 窗体加载后即最小化到托盘区
301
private void WatchService_Load(object sender, System.EventArgs e)
302

{
303
this.Hide();
304
this.notifyIcon1.Visible=true;
305
startService();
306
}
307
// 窗体最小化
308
private void WatchService_Resize(object sender, System.EventArgs e)
309

{
310
if (this.windowstate==Formwindowstate.Minimized)
311

{
312
this.Hide();
313
this.notifyIcon1.Visible=true;
314
}
315
}
316
// 托盘区图标操作
317
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
318

{
319
this.Visible = true;
320
this.windowstate = Formwindowstate.Normal;
321
this.notifyIcon1.Visible = false;
322
}
323
// 停止监控
324
private void btn_stopWatch_Click(object sender, System.EventArgs e)
325

{
326
this.timer1.Stop();
327
}
328
}
329
}
posted on 2009-06-11 13:29
Deo.Feng 阅读(390)
评论(0) 编辑 收藏