C# Window Service详解

Windows Service这一块并不复杂,可是注意事项太多了,网上资料也很凌乱,偶尔本身写也会丢三落四的。因此本文也就产生了,本文不会写复杂的东西,彻底以基础应用的需求来写,因此不会对Windows Service写很深刻。spa

本文介绍了如何用C#建立、安装、启动、监控、卸载简单的Windows Service 的内容步骤和注意事项。调试

1、建立一个Windows Service

1)建立Windows Service项目

imageimage

2)对Service重命名

将Service1重命名为你服务名称,这里咱们命名为ServiceTest。code

2、建立服务安装程序

1)添加安装程序

image

image

以后咱们能够看到上图,自动为咱们建立了ProjectInstaller.cs以及2个安装的组件。blog

2)修改安装服务名

右键serviceInsraller1,选择属性,将ServiceName的值改成ServiceTest。进程

image

3)修改安装权限

右键serviceProcessInsraller1,选择属性,将Account的值改成LocalSystem。get

image

3、建立安装脚本

在项目中添加2个文件以下(必须是ANSI或者UTF-8无BOM格式):it

1)安装脚本Install.bat

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe WindowsServiceTest.exe

Net Start ServiceTest

sc config ServiceTest start= auto

2)卸载脚本Uninstall.bat

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u WindowsServiceTest.exe

3)安装脚本说明

第二行为启动服务。class

第三行为设置服务为自动运行。监控

这2行视服务形式自行选择。基础

4)脚本调试

若是须要查看脚本运行情况,在脚本最后一行加入pause

4、调试Windows Service

1)安装并运行服务

2)附加进程

imageimage

3)在代码中加入断点进行调试

image

相关文章
相关标签/搜索