使用C#建立WINDOWS服务
1、创建WINDOWS服务代码
2、客户端

1
using System;
2
using System.IO ;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Data;
6
using System.Diagnostics;
7
using System.ServiceProcess;
8
9
namespace MyService
10

{
11
public class MyServiceTest : System.ServiceProcess.ServiceBase
12
{
13
private System.Diagnostics.EventLog eventLog1;
14
private System.Diagnostics.PerformanceCounter performanceCounter2;
15
/**//// <summary>
16
/// 必需的设计器变量。
17
/// </summary>
18
private System.ComponentModel.Container components = null;
19
20
public MyServiceTest()
21
{
22
// 该调用是 Windows.Forms 组件设计器所必需的。
23
InitializeComponent();
24
25
// TODO: 在 InitComponent 调用后添加任何初始化
26
}
27
28
// 进程的主入口点
29
static void Main()
30
{
31
System.ServiceProcess.ServiceBase[] ServicesToRun;
32
33
// 同一进程中可以运行多个用户服务。若要将
34
//另一个服务添加到此进程,请更改下行
35
// 以创建另一个服务对象。例如,
36
//
37
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
38
//
39
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{ new MyServiceTest() };
40
41
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
42
}
43
44
/**//// <summary>
45
/// 设计器支持所需的方法 - 不要使用代码编辑器
46
/// 修改此方法的内容。
47
/// </summary>
48
private void InitializeComponent()
49
{
50
this.eventLog1 = new System.Diagnostics.EventLog();
51
this.performanceCounter2 = new System.Diagnostics.PerformanceCounter();
52
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
53
((System.ComponentModel.ISupportInitialize)(this.performanceCounter2)).BeginInit();
54
//
55
// eventLog1
56
//
57
this.eventLog1.Log = "MyServiceLog2";
58
this.eventLog1.Source = "MyService2";
59
//
60
// performanceCounter2
61
//
62
this.performanceCounter2.CategoryName = "MyTestCount";
63
this.performanceCounter2.CounterName = "RunCount";
64
this.performanceCounter2.MachineName = "zxd";
65
//
66
// MyServiceTest
67
//
68
this.AutoLog = false;
69
this.CanPauseAndContinue = true;
70
this.ServiceName = "MyServiceTest";
71
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
72
((System.ComponentModel.ISupportInitialize)(this.performanceCounter2)).EndInit();
73
74
}
75
76
/**//// <summary>
77
/// 清理所有正在使用的资源。
78
/// </summary>
79
protected override void Dispose( bool disposing )
80
{
81
if( disposing )
82
{
83
if (components != null)
84
{
85
components.Dispose();
86
}
87
}
88
base.Dispose( disposing );
89
}
90
91
/**//// <summary>
92
/// 设置具体的操作,以便服务可以执行它的工作。
93
/// </summary>
94
protected override void OnStart(string[] args)
95
{
96
// TODO: 在此处添加代码以启动服务。
97
98
createlog();
99
performanceCounter2.IncrementBy(2);
100
101
eventLog1.WriteEntry("My service started ("+performanceCounter2.RawValue.ToString()+")",EventLogEntryType.Information );
102
103
string fname=@"c:\myservice_test.txt";
104
105
FileStream fs=new FileStream(fname,
106
(File.Exists(fname))?FileMode.Append:FileMode.Create,
107
FileAccess.Write);
108
109
StreamWriter sw=new StreamWriter(fs);
110
//sw.a
111
sw.WriteLine("My service start:"+DateTime.Now.ToString());
112
sw.Close();
113
fs.Close();
114
}
115
116
/**//// <summary>
117
/// 停止此服务。
118
/// </summary>
119
protected override void OnStop()
120
{
121
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
122
createlog();
123
eventLog1.WriteEntry("My service stoped",EventLogEntryType.Information );
124
125
string fname=@"c:\myservice_test.txt";
126
127
FileStream fs=new FileStream(fname,
128
(File.Exists(fname))?FileMode.Append:FileMode.Create,
129
FileAccess.Write);
130
131
StreamWriter sw=new StreamWriter(fs);
132
sw.WriteLine("My service stop:"+DateTime.Now.ToString());
133
sw.Close();
134
fs.Close();
135
}
136
private void createlog()
{
137
// if (EventLog.SourceExists("MyServicelog2")){
138
// EventLog.CreateEventSource("MyService2","MyServicelog2");
139
// }
140
// eventLog1.Source ="MyServiceTest";
141
//这里获得的经验,当我添加应用程序日志测试成功后。我尝试添加自定义日志
142
//结果总是提示已存在MyService源,原来只要日志中写入过这个源名称
143
//就不能在自定义日志分类中使用此源
144
//另外上述注释掉的代码是手动编码添加自定义源,现在则在设计器中完成
145
}
146
147
protected override void OnCustomCommand(int command)
148
{
149
// TODO: 添加 MyServiceTest.OnCustomCommand 实现
150
//base.OnCustomCommand (command);
151
createlog();
152
eventLog1.WriteEntry("My service command "+command.ToString(),EventLogEntryType.Information );
153
154
if (command==128)
{
155
156
string s=performanceCounter2.RawValue.ToString() ;
157
string fname=@"c:\myservice_test.txt";
158
159
FileStream fs=new FileStream(fname,
160
(File.Exists(fname))?FileMode.Append:FileMode.Create,
161
FileAccess.Write);
162
163
StreamWriter sw=new StreamWriter(fs);
164
sw.WriteLine("My service command:"+command.ToString()+" Return value: "+s+DateTime.Now.ToString());
165
sw.Close();
166
fs.Close();
167
168
}
169
}
170
171
private void eventLog1_EntryWritten(object sender, System.Diagnostics.EntryWrittenEventArgs e)
172
{
173
//
174
}
175
}
176
}
177
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
2、客户端
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.Configuration.Install ;
8
using System.Diagnostics;
9
10
11
namespace XmlTest
12

{
13
/**//// <summary>
14
/// Form1 的摘要说明。
15
/// </summary>
16
public class Form1 : System.Windows.Forms.Form
17
{
18
private System.Windows.Forms.Button btStart;
19
private System.Windows.Forms.Button btStop;
20
private System.Windows.Forms.Button btInstall;
21
private System.Windows.Forms.Button btUninstall;
22
private System.Windows.Forms.Label label1;
23
private System.Windows.Forms.Label lbstatus;
24
/**//// <summary>
25
/// 必需的设计器变量。
26
/// </summary>
27
private System.ComponentModel.Container components = null;
28
private System.ServiceProcess.ServiceController sc=null;
29
private System.Windows.Forms.Label lbflag;
30
private System.ServiceProcess.ServiceControllerStatus runstatus ;
31
private System.Windows.Forms.GroupBox groupBox1;
32
private System.Windows.Forms.TextBox txtMsg;
33
private string strerr;
34
private System.Windows.Forms.OpenFileDialog openFD;
35
private System.Windows.Forms.ProgressBar pBar1;
36
private System.Windows.Forms.Button btPause;
37
private System.Windows.Forms.Button bt128;
38
private System.Windows.Forms.Button bt129;
39
private bool installstatus=false;
40
41
public Form1()
42
{
43
//
44
// Windows 窗体设计器支持所必需的
45
//
46
InitializeComponent();
47
48
//
49
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
50
//
51
}
52
53
/**//// <summary>
54
/// 清理所有正在使用的资源。
55
/// </summary>
56
protected override void Dispose( bool disposing )
57
{
58
if( disposing )
59
{
60
if (components != null)
61
{
62
components.Dispose();
63
}
64
}
65
base.Dispose( disposing );
66
}
67
68
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
69
/**//// <summary>
70
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
71
/// 此方法的内容。
72
/// </summary>
73
private void InitializeComponent()
74
{
75
this.btStart = new System.Windows.Forms.Button();
76
this.btStop = new System.Windows.Forms.Button();
77
this.btInstall = new System.Windows.Forms.Button();
78
this.btUninstall = new System.Windows.Forms.Button();
79
this.label1 = new System.Windows.Forms.Label();
80
this.lbflag = new System.Windows.Forms.Label();
81
this.lbstatus = new System.Windows.Forms.Label();
82
this.groupBox1 = new System.Windows.Forms.GroupBox();
83
this.txtMsg = new System.Windows.Forms.TextBox();
84
this.openFD = new System.Windows.Forms.OpenFileDialog();
85
this.pBar1 = new System.Windows.Forms.ProgressBar();
86
this.btPause = new System.Windows.Forms.Button();
87
this.bt128 = new System.Windows.Forms.Button();
88
this.bt129 = new System.Windows.Forms.Button();
89
this.groupBox1.SuspendLayout();
90
this.SuspendLayout();
91
//
92
// btStart
93
//
94
this.btStart.Font = new System.Drawing.Font("Arial", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
95
this.btStart.ForeColor = System.Drawing.Color.RoyalBlue;
96
this.btStart.Location = new System.Drawing.Point(16, 80);
97
this.btStart.Name = "btStart";
98
this.btStart.TabIndex = 0;
99
this.btStart.Text = "Start";
100
this.btStart.Click += new System.EventHandler(this.btStart_Click);
101
//
102
// btStop
103
//
104
this.btStop.Font = new System.Drawing.Font("Arial", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
105
this.btStop.ForeColor = System.Drawing.Color.RoyalBlue;
106
this.btStop.Location = new System.Drawing.Point(176, 80);
107
this.btStop.Name = "btStop";
108
this.btStop.TabIndex = 1;
109
this.btStop.Text = "Stop";
110
this.btStop.Click += new System.EventHandler(this.btStop_Click);
111
//
112
// btInstall
113
//
114
this.btInstall.Font = new System.Drawing.Font("Arial", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
115
this.btInstall.ForeColor = System.Drawing.Color.RoyalBlue;
116
this.btInstall.Location = new System.Drawing.Point(16, 48);
117
this.btInstall.Name = "btInstall";
118
this.btInstall.TabIndex = 2;
119
this.btInstall.Text = "Install";
120
this.btInstall.Click += new System.EventHandler(this.btInstall_Click);
121
//
122
// btUninstall
123
//
124
this.btUninstall.Font = new System.Drawing.Font("Arial", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
125
this.btUninstall.ForeColor = System.Drawing.Color.RoyalBlue;
126
this.btUninstall.Location = new System.Drawing.Point(96, 48);
127
this.btUninstall.Name = "btUninstall";
128
this.btUninstall.TabIndex = 3;
129
this.btUninstall.Text = "Uninstall";
130
this.btUninstall.Click += new System.EventHandler(this.btUninstall_Click);
131
//
132
// label1
133
//
134
this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
135
this.label1.Location = new System.Drawing.Point(16, 16);
136
this.label1.Name = "label1";
137
this.label1.Size = new System.Drawing.Size(136, 23);
138
this.label1.TabIndex = 4;
139
this.label1.Text = "MyService Status:";
140
//
141
// lbflag
142
//
143
this.lbflag.Font = new System.Drawing.Font("Wingdings", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(2)));
144
this.lbflag.ForeColor = System.Drawing.Color.ForestGreen;
145
this.lbflag.Location = new System.Drawing.Point(160, 16);
146
this.lbflag.Name = "lbflag";
147
this.lbflag.Size = new System.Drawing.Size(16, 16);
148
this.lbflag.TabIndex = 5;
149
this.lbflag.Text = "v";
150
//
151
// lbstatus
152
//
153
this.lbstatus.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
154
this.lbstatus.Location = new System.Drawing.Point(184, 16);
155
this.lbstatus.Name = "lbstatus";
156
this.lbstatus.Size = new System.Drawing.Size(200, 23);
157
this.lbstatus.TabIndex = 6;
158
this.lbstatus.Text = "Running";
159
//
160
// groupBox1
161
//
162
this.groupBox1.Controls.Add(this.txtMsg);
163
this.groupBox1.Location = new System.Drawing.Point(16, 176);
164
this.groupBox1.Name = "groupBox1";
165
this.groupBox1.Size = new System.Drawing.Size(384, 120);
166
this.groupBox1.TabIndex = 7;
167
this.groupBox1.TabStop = false;
168
this.groupBox1.Text = "Message";
169
//
170
// txtMsg
171
//
172
this.txtMsg.Dock = System.Windows.Forms.DockStyle.Fill;
173
this.txtMsg.Location = new System.Drawing.Point(3, 17);
174
this.txtMsg.Multiline = true;
175
this.txtMsg.Name = "txtMsg";
176
this.txtMsg.ScrollBars = System.Windows.Forms.ScrollBars.Both;
177
this.txtMsg.Size = new System.Drawing.Size(378, 100);
178
this.txtMsg.TabIndex = 0;
179
this.txtMsg.Text = "";
180
//
181
// openFD
182
//
183
this.openFD.Filter = "服务执行文件(*.exe)|*.exe";
184
this.openFD.Title = "服务测试文件定位";
185
//
186
// pBar1
187
//
188
this.pBar1.Location = new System.Drawing.Point(24, 152);
189
this.pBar1.Name = "pBar1";
190
this.pBar1.Size = new System.Drawing.Size(368, 16);
191
this.pBar1.TabIndex = 8;
192
//
193
// btPause
194
//
195
this.btPause.Font = new System.Drawing.Font("Arial", 10.5F, System.Drawing.FontStyle.Bold);
196
this.btPause.ForeColor = System.Drawing.Color.RoyalBlue;
197
this.btPause.Location = new System.Drawing.Point(96, 80);
198
this.btPause.Name = "btPause";
199
this.btPause.TabIndex = 9;
200
this.btPause.Text = "Pause";
201
this.btPause.Click += new System.EventHandler(this.btPause_Click);
202
//
203
// bt128
204
//
205
this.bt128.Font = new System.Drawing.Font("Arial", 10.5F, System.Drawing.FontStyle.Bold);
206
this.bt128.ForeColor = System.Drawing.Color.RoyalBlue;
207
this.bt128.Location = new System.Drawing.Point(16, 112);
208
this.bt128.Name = "bt128";
209
this.bt128.Size = new System.Drawing.Size(112, 23);
210
this.bt128.TabIndex = 10;
211
this.bt128.Text = "Execute 128";
212
this.bt128.Click += new System.EventHandler(this.bt128_Click);
213
//
214
// bt129
215
//
216
this.bt129.Font = new System.Drawing.Font("Arial", 10.5F, System.Drawing.FontStyle.Bold);
217
this.bt129.ForeColor = System.Drawing.Color.RoyalBlue;
218
this.bt129.Location = new System.Drawing.Point(136, 112);
219
this.bt129.Name = "bt129";
220
this.bt129.Size = new System.Drawing.Size(112, 23);
221
this.bt129.TabIndex = 11;
222
this.bt129.Text = "Execute 129";
223
this.bt129.Click += new System.EventHandler(this.bt129_Click);
224
//
225
// Form1
226
//
227
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
228
this.ClientSize = new System.Drawing.Size(416, 307);
229
this.Controls.Add(this.bt129);
230
this.Controls.Add(this.bt128);
231
this.Controls.Add(this.btPause);
232
this.Controls.Add(this.pBar1);
233
this.Controls.Add(this.groupBox1);
234
this.Controls.Add(this.lbstatus);
235
this.Controls.Add(this.lbflag);
236
this.Controls.Add(this.label1);
237
this.Controls.Add(this.btUninstall);
238
this.Controls.Add(this.btInstall);
239
this.Controls.Add(this.btStop);
240
this.Controls.Add(this.btStart);
241
this.Name = "Form1";
242
this.Text = "Form1";
243
this.Load += new System.EventHandler(this.Form1_Load);
244
this.groupBox1.ResumeLayout(false);
245
this.ResumeLayout(false);
246
247
}
248
#endregion
249
250
/**//// <summary>
251
/// 应用程序的主入口点。
252
/// </summary>
253
[STAThread]
254
static void Main()
255
{
256
Application.Run(new Form1());
257
}
258
259
private void btStart_Click(object sender, System.EventArgs e)
260
{
261
sc=new System.ServiceProcess.ServiceController();
262
sc.MachineName ="zxd";
263
sc.ServiceName ="MyServiceTest";
264
265
266
if (sc.Status !=System.ServiceProcess.ServiceControllerStatus.Running )
{
267
sc.Start();
268
}
269
if (!GetServiceStatus())
270
{
271
txtMsg.Text+=strerr+Environment.NewLine ;
272
}
273
}
274
275
private void btStop_Click(object sender, System.EventArgs e)
276
{
277
sc=new System.ServiceProcess.ServiceController();
278
sc.MachineName ="zxd";
279
sc.ServiceName ="MyServiceTest";
280
281
if (sc.Status !=System.ServiceProcess.ServiceControllerStatus.Stopped )
282
{
283
sc.Stop();
284
}
285
if (!GetServiceStatus())
286
{
287
txtMsg.Text+=strerr+Environment.NewLine ;
288
}
289
}
290
291
private void Form1_Load(object sender, System.EventArgs e)
292
{
293
//获取服务运行状态
294
btInstall.Enabled =false;
295
btUninstall.Enabled =false;
296
btStart.Enabled =false;
297
btStop.Enabled =false;
298
btPause.Enabled =false;
299
300
if (!GetServiceStatus())
301
{
302
txtMsg.Text+=strerr+Environment.NewLine ;
303
}
304
305
}
306
private bool GetServiceStatus()
{
307
//等待5秒以后开始
308
pBar1.Value =0;
309
for(int i=0;i<5;i++)
310
{
311
System.Threading.Thread.Sleep(1000);
312
pBar1.Value =(i+1)*100/5;
313
}
314
//图简便,应使用 sc.WaitForStatus()
315
316
try
317
{
318
sc=new System.ServiceProcess.ServiceController();
319
sc.MachineName ="zxd";
320
sc.ServiceName ="MyServiceTest";
321
this.runstatus=sc.Status ;
322
323
if (runstatus==System.ServiceProcess.ServiceControllerStatus.Running )
324
{
325
lbflag.Text ="l";
326
}
327
else if (runstatus==System.ServiceProcess.ServiceControllerStatus.Stopped)
328
{
329
lbflag.Text ="n";
330
}
331
else
332
{
333
lbflag.Text ="u";
334
}
335
lbstatus.Text =runstatus.ToString ();
336
installstatus=true;
337
return true;
338
}
339
catch(Exception ex)
340
{
341
strerr="Error:"+ex.Message;
342
lbflag.Text ="v";
343
lbstatus.Text="Uninstall";
344
installstatus=false;
345
return false;
346
}
347
finally
{
348
ButtonFace();
349
sc.Close();
350
}
351
352
353
}
354
private void ButtonFace()
{
355
if (this.installstatus ==true)
356
{
357
btInstall.Enabled =false;
358
btUninstall.Enabled =true;
359
if (runstatus==System.ServiceProcess.ServiceControllerStatus.Running )
360
{
361
btStart.Enabled =false;
362
btStop.Enabled =true;
363
btPause.Enabled =true;
364
}
365
else if(runstatus==System.ServiceProcess.ServiceControllerStatus.Stopped)
366
{
367
btStart.Enabled =true;
368
btPause.Enabled =false;
369
btStop.Enabled =false;
370
}
371
else if(runstatus==System.ServiceProcess.ServiceControllerStatus.Paused )
372
{
373
btStart.Enabled =true;
374
btPause.Enabled =false;
375
btStop.Enabled =true;
376
}
377
else
{
378
btStart.Enabled =false;
379
btPause.Enabled =false;
380
btStop.Enabled =false;
381
}
382
}
383
else
{
384
btInstall.Enabled =true;
385
btUninstall.Enabled =false;
386
btStart.Enabled =false;
387
btStop.Enabled =false;
388
btPause.Enabled =false;
389
}
390
}
391
392
private void btInstall_Click(object sender, System.EventArgs e)
393
{
394
string tagfile="";
395
openFD.FileName="";
396
openFD.ShowDialog();
397
398
if (openFD.FileName!=String.Empty )
{
399
tagfile=openFD.FileName;
400
//这里要使用Process类创建一个进程,并且实现命令行注册服务
401
402
Process p = new Process();
403
404
//实例一个Process类,启动一个独立进程
405
//Process类有一个StartInfo属性,这个是ProcessStartInfo类,
406
//包括了一些属性和方法,下面我们用到了他的几个属性
407
408
//设定程序名
409
410
p.StartInfo.FileName = "cmd.exe";
411
412
//关闭Shell的使用
413
414
p.StartInfo.UseShellExecute = false;
415
416
//重定向标准输入
417
418
p.StartInfo.RedirectStandardInput = true;
419
420
//重定向标准输出
421
422
p.StartInfo.RedirectStandardOutput = true;
423
424
//重定向错误输出
425
426
p.StartInfo.RedirectStandardError = true;
427
428
//设置不显示窗口
429
430
p.StartInfo.CreateNoWindow = true;
431
432
//上面几个属性的设置是比较关键的一步。
433
434
//既然都设置好了那就启动进程吧,
435
436
p.Start();
437
438
//输入要执行的命令,这里就是 installutil.exe 服务程序名称.exe
439
440
p.StandardInput.WriteLine("installutil.exe "+tagfile);
441
p.StandardInput.WriteLine("exit");
442
443
//从输出流获取命令执行结果,
444
string strRst = p.StandardOutput.ReadToEnd();
445
446
txtMsg.Text =strRst;
447
448
449
if (!GetServiceStatus())
450
{
451
txtMsg.Text+=strerr+Environment.NewLine ;
452
}
453
454
455
456
457
458
459
460
}
461
462
}
463
464
private void btUninstall_Click(object sender, System.EventArgs e)
465
{
466
string tagfile=@" D:\MCSD.NET\70-320\Test\MyService\bin\Debug\MyService.exe";
467
//这里要使用Process类创建一个进程,并且实现命令行注册服务
468
469
Process p = new Process();
470
471
//实例一个Process类,启动一个独立进程
472
//Process类有一个StartInfo属性,这个是ProcessStartInfo类,
473
//包括了一些属性和方法,下面我们用到了他的几个属性
474
475
//设定程序名
476
477
p.StartInfo.FileName = "cmd.exe";
478
479
//关闭Shell的使用
480
481
p.StartInfo.UseShellExecute = false;
482
483
//重定向标准输入
484
485
p.StartInfo.RedirectStandardInput = true;
486
487
//重定向标准输出
488
489
p.StartInfo.RedirectStandardOutput = true;
490
491
//重定向错误输出
492
493
p.StartInfo.RedirectStandardError = true;
494
495
//设置不显示窗口
496
497
p.StartInfo.CreateNoWindow = true;
498
499
//上面几个属性的设置是比较关键的一步。
500
501
//既然都设置好了那就启动进程吧,
502
503
p.Start();
504
505
//输入要执行的命令,这里就是 installutil.exe 服务程序名称.exe
506
507
p.StandardInput.WriteLine("installutil.exe /u"+tagfile);
508
p.StandardInput.WriteLine("exit");
509
510
//从输出流获取命令执行结果,
511
string strRst = p.StandardOutput.ReadToEnd();
512
513
txtMsg.Text =strRst;
514
515
516
if (!GetServiceStatus())
517
{
518
txtMsg.Text+=strerr+Environment.NewLine ;
519
}
520
}
521
522
private void btPause_Click(object sender, System.EventArgs e)
523
{
524
sc=new System.ServiceProcess.ServiceController();
525
sc.MachineName ="zxd";
526
sc.ServiceName ="MyServiceTest";
527
528
if (sc.Status ==System.ServiceProcess.ServiceControllerStatus.Running )
529
{
530
sc.Pause ();
531
}
532
if (!GetServiceStatus())
533
{
534
txtMsg.Text+=strerr+Environment.NewLine ;
535
}
536
}
537
538
private void bt128_Click(object sender, System.EventArgs e)
539
{
540
//调用服务的128命令
541
sc=new System.ServiceProcess.ServiceController();
542
sc.MachineName ="zxd";
543
sc.ServiceName ="MyServiceTest";
544
sc.ExecuteCommand(128);
545
}
546
547
private void bt129_Click(object sender, System.EventArgs e)
548
{
549
//调用服务的129命令
550
sc=new System.ServiceProcess.ServiceController();
551
sc.MachineName ="zxd";
552
sc.ServiceName ="MyServiceTest";
553
sc.ExecuteCommand(129);
554
}
555
}
556
}
557
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
转载于:https://www.cnblogs.com/coldwine/archive/2005/07/31/204269.html