-
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的。所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Windows Service写很深入。
本文介绍了如何用C#创建、安装、启动、监控、卸载简单的Windows Service 的内容步骤和注意事项。
一、创建一个Windows Service
1)创建Windows Service项目
2)对Service重命名
将Service1重命名为你服务名称,这里我们命名为ServiceTest。二、创建服务安装程序
1)添加安装程序
之后我们可以看到上图,自动为我们创建了ProjectInstaller.cs以及2个安装的组件。
2)修改安装服务名
右键serviceInsraller1,选择属性,将ServiceName的值改为ServiceTest。
3)修改安装权限
右键serviceProcessInsraller1,选择属性,将Account的值改为LocalSystem。
三、写入服务代码
1)打开ServiceTest代码
右键ServiceTest,选择查看代码。2)写入Service逻辑
添加如下代码:01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.ComponentModel;
04.
using
System.Data;
05.
using
System.Diagnostics;
06.
using
System.Linq;
07.
using
System.ServiceProcess;
08.
using
System.Text;
09.
10.
namespace
WindowsServiceTest
11.
{
12.
public
partial
class
ServiceTest : ServiceBase
13.
{
14.
public
ServiceTest()
15.
{
16.
InitializeComponent();
17.
}
18.
19.
protected
override
void
OnStart(
string
[] args)
20.
{
21.
using
(System.IO.StreamWriter sw =
new
System.IO.StreamWriter(
"C:\\log.txt"
,
true
))
22.
{
23.
sw.WriteLine(DateTime.Now.ToString(
"yyyy-MM-dd HH:mm:ss "
) +
"Start."
);
24.
}
25.
}
26.
27.
protected
override
void
OnStop()
28.
{
29.
using
(System.IO.StreamWriter sw =
new
System.IO.StreamWriter(
"C:\\log.txt"
,
true
))
30.
{
31.
sw.WriteLine(DateTime.Now.ToString(
"yyyy-MM-dd HH:mm:ss "
) +
"Stop."
);
32.
}
33.
}
34.
}
35.
}
这里我们的逻辑很简单,启动服务的时候写个日志,关闭的时候再写个日志。
四、创建安装脚本
在项目中添加2个文件如下(必须是ANSI或者UTF-8无BOM格式):1)安装脚本Install.bat
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe WindowsServiceTest.exe
Net Start ServiceTest
sc config ServiceTest start= auto2)卸载脚本Uninstall.bat
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u WindowsServiceTest.exe3)安装脚本说明
第二行为启动服务。第三行为设置服务为自动运行。
这2行视服务形式自行选择。
4)脚本调试
如果需要查看脚本运行状况,在脚本最后一行加入pause五、在C#中对服务进行控制
0)配置目录结构
简历一个新WPF项目,叫WindowsServiceTestUI,添加对System.ServiceProcess的引用。在WindowsServiceTestUI的bin\Debug目录下建立Service目录。
将WindowsServiceTest的生成目录设置为上面创建的Service目录。
生成后目录结构如下图
1)安装
安装时会产生目录问题,所以安装代码如下:1.
string
CurrentDirectory = System.Environment.CurrentDirectory;
2.
System.Environment.CurrentDirectory = CurrentDirectory +
"\\Service"
;
3.
Process process =
new
Process();
4.
process.StartInfo.UseShellExecute =
false
;
5.
process.StartInfo.FileName =
"Install.bat"
;
6.
process.StartInfo.CreateNoWindow =
true
;
7.
process.Start();
8.
System.Environment.CurrentDirectory = CurrentDirectory;
2)卸载
卸载时也会产生目录问题,所以卸载代码如下:1.
string
CurrentDirectory = System.Environment.CurrentDirectory;
2.
System.Environment.CurrentDirectory = CurrentDirectory +
"\\Service"
;
3.
Process process =
new
Process();
4.
process.StartInfo.UseShellExecute =
false
;
5.
process.StartInfo.FileName =
"Uninstall.bat"
;
6.
process.StartInfo.CreateNoWindow =
true
;
7.
process.Start();
8.
System.Environment.CurrentDirectory = CurrentDirectory;
3)启动
代码如下:1.
using
System.ServiceProcess;
2.
ServiceController serviceController =
new
ServiceController(
"ServiceTest"
);
3.
serviceController.Start();
4)停止
1.
ServiceController serviceController =
new
ServiceController(
"ServiceTest"
);
2.
if
(serviceController.CanStop)
3.
serviceController.Stop();
5)暂停/继续
1.
ServiceController serviceController =
new
ServiceController(
"ServiceTest"
);
2.
if
(serviceController.CanPauseAndContinue)
3.
{
4.
if
(serviceController.Status == ServiceControllerStatus.Running)
5.
serviceController.Pause();
6.
else
if
(serviceController.Status == ServiceControllerStatus.Paused)
7.
serviceController.Continue();
8.
}
6)检查状态
1.
ServiceController serviceController =
new
ServiceController(
"ServiceTest"
);
2.
string
Status = serviceController.Status.ToString();
六、调试Windows Service
1)安装并运行服务
2)附加进程
3)在代码中加入断点进行调试
七、总结
本文对Windows service的上述配置都未做详细解释,但是按上述步骤就可以制作可运行的Windows Service,从而达到了工作的需求。
C#创建Windows Service(Windows 服务)基础教程
最新推荐文章于 2024-03-18 09:28:21 发布