C#创建windows服务列表
转载自:http://www.cnblogs.com/sorex/archive/2012/05/16/2502001.html
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逻辑
添加如下代码:
1
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
|
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; namespace WindowsServiceTest { public partial class ServiceTest : ServiceBase { public ServiceTest() { InitializeComponent(); } protected override void OnStart( string [] args) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter( "C:\\log.txt" , true )) { sw.WriteLine(DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss " ) + "Start." ); } } protected override void OnStop() { using (System.IO.StreamWriter sw = new System.IO.StreamWriter( "C:\\log.txt" , true )) { sw.WriteLine(DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss " ) + "Stop." ); } } } } |
这里我们的逻辑很简单,启动服务的时候写个日志,关闭的时候再写个日志。
四、创建安装脚本
在项目中添加2个文件如下(必须是ANSI或者UTF-8无BOM格式):
1)安装脚本Install.bat
1
2
3
|
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe WindowsServiceTest.exe Net Start ServiceTest sc config ServiceTest start= auto |
2)卸载脚本Uninstall.bat
1
|
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u WindowsServiceTest.exe |
3)安装脚本说明
第二行为启动服务。
第三行为设置服务为自动运行。
这2行视服务形式自行选择。
4)脚本调试
如果需要查看脚本运行状况,在脚本最后一行加入pause
五、在C#中对服务进行控制
0)配置目录结构
简历一个新WPF项目,叫WindowsServiceTestUI,添加对System.ServiceProcess的引用。
在WindowsServiceTestUI的bin\Debug目录下建立Service目录。
将WindowsServiceTest的生成目录设置为上面创建的Service目录。
生成后目录结构如下图
1)安装
安装时会产生目录问题,所以安装代码如下:
1
2
3
4
5
6
7
8
|
string CurrentDirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = CurrentDirectory + "\\Service" ; Process process = new Process(); process.StartInfo.UseShellExecute = false ; process.StartInfo.FileName = "Install.bat" ; process.StartInfo.CreateNoWindow = true ; process.Start(); System.Environment.CurrentDirectory = CurrentDirectory; |
2)卸载
卸载时也会产生目录问题,所以卸载代码如下:
1
2
3
4
5
6
7
8
|
string CurrentDirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = CurrentDirectory + "\\Service" ; Process process = new Process(); process.StartInfo.UseShellExecute = false ; process.StartInfo.FileName = "Uninstall.bat" ; process.StartInfo.CreateNoWindow = true ; process.Start(); System.Environment.CurrentDirectory = CurrentDirectory; |
3)启动
代码如下:
1
2
3
4
5
|
using System.ServiceProcess; ServiceController serviceController = new ServiceController( "ServiceTest" ); serviceController.Start(); |
4)停止
1
2
3
|
ServiceController serviceController = new ServiceController( "ServiceTest" ); if (serviceController.CanStop) serviceController.Stop(); |
5)暂停/继续
1
2
3
4
5
6
7
8
|
ServiceController serviceController = new ServiceController( "ServiceTest" ); if (serviceController.CanPauseAndContinue) { if (serviceController.Status == ServiceControllerStatus.Running) serviceController.Pause(); else if (serviceController.Status == ServiceControllerStatus.Paused) serviceController.Continue(); } |
6)检查状态
1
2
|
ServiceController serviceController = new ServiceController( "ServiceTest" ); string Status = serviceController.Status.ToString(); |
六、调试Windows Service
1)安装并运行服务
2)附加进程
3)在代码中加入断点进行调试
七、总结
本文对Windows service的上述配置都未做详细解释,但是按上述步骤就可以制作可运行的Windows Service,从而达到了工作的需求。
示例代码请见:https://github.com/sorex/WindowsServiceTest
C#创建windows服务列表的更多相关文章
- C#/.NET基于Topshelf创建Windows服务程序及服务的安装和卸载(极速,简洁)
本文首发于:码友网--一个专注.NET/.NET Core开发的编程爱好者社区. 文章目录 C#/.NET基于Topshelf创建Windows服务的系列文章目录: C#/.NET基于Topshelf ...
- C# — 创建Windows服务
以前从来没有接触过C#,对Windows服务也完全不了解,今天通过使用VS2017创建了一个Windows服务,并进行了安装和卸载,目前也是一知半解的地步,简单的做个笔记记录一下,也算是复习了吧. 第 ...
- 使用 Topshelf 创建 Windows 服务
Ø 前言 C# 创建 Windows 服务的方式有很多种,Topshelf 就是其中一种方式,而且使用起来比较简单.下面使用 Visual Studio Ultimate 2013 演示一下具体的使 ...
- 使用C#创建windows服务续之使用Topshelf优化Windows服务
前言: 之前写了一篇“使用C#创建windows服务”,https://www.cnblogs.com/huangwei1992/p/9693167.html,然后有博友给我推荐了一个开源框架Tops ...
- VS2013创建Windows服务与调试服务
1.创建Windows服务 说明: a)Description 服务描述,直接显示到Windows服务列表中的描述: b)DisplayName 服务显示名称,直接显示到Windows服务列表中的名称 ...
- 用C#创建Windows服务(Windows Services)
用C#创建Windows服务(Windows Services) 学习: 第一步:创建服务框架 创建一个新的 Windows 服务项目,可以从Visual C# 工程中选取 Windows 服务(W ...
- 玩转Windows服务系列——创建Windows服务
创建Windows服务的项目 新建项目->C++语言->ATL->ATL项目->服务(EXE) 这样就创建了一个Windows服务项目. 生成的解决方案包含两个项目:Servi ...
- .Net创建windows服务入门
本文主要记录学习.net 如何创建windows服务. 1.创建一个Windows服务程序 2.新建安装程序 3.修改service文件 代码如下 protected override void On ...
- C# 创建Windows服务
创建windows服务项目 2 右键点击Service1.cs,查看代码, 用于编写操作逻辑代码 3 代码中OnStart用于执行服务事件,一般采用线程方式执行方法,便于隔一段事件执行一回 END ...
随机推荐
- hdu 4403 dfs
巨麻烦的模拟题orz.... 先确定等号的位置,然后两层dfs,分别算等号前面的和后面的,再比较 话说这题竟然不开long long都能水过 #include <iostream> #in ...
- 正则表达式——语法
正则表达式(regular expression)--描述一种字符串匹配模式,可以用来检测一个字符串是否包含特定的子串.用其他字符串将其代替.提取出某个符合要求的子串. 正则表达式 由普通字符 和 ...
- 一个关于AdaBoost算法的简单证明
下载本文PDF格式(Academia.edu) 本文给出了机器学习中AdaBoost算法的一个简单初等证明,需要使用的数学工具为微积分-1. Adaboost is a powerful algori ...
- centos7安装mplayer的方法
首先是要获取源代码. 首先是主程序的源代码. 打开你的终端,按照我的命令一步一步来: cd Download svn checkout svn://svn.mplayerhq.hu/mplayer/t ...
- [Android]Message,MessageQueue,Looper,Handler详解+实例
转http://www.eoeandroid.com/forum-viewthread-tid-49595-highlight-looper.html 一.几个关键概念 1.MessageQueue: ...
- 【Alpha阶段】第一次线上会议
会议信息 因编译作业ddl,暂时没有大进展,没有close的issue 时间:2016.11.07 19:00 时长:10min 地点:讨论组 类型:线上会议 NXT:2016.11.08 21:30 ...
- iOS - 如何自动播放H5中的音频
场景:iOS端设备,App页面跳转到H5产品介绍,背景音乐无法播放.(为什么不能自动播放,因该是iPhone人性化设定吧~) 加载H5用UIWebView空间: 代码: CGRect rect = s ...
- IOS VFL屏幕自适应
-(void)fun1{ //注意使用VFL,不用设置视图的frame UIView *view = [[UIView alloc] init]; view.backgroundColor = [UI ...
- RAL 标准颜色表(RAL Color Chart)
根据经典RAL系统,本网页显示RAL标准颜色的概览.RAL用于信息,为图画和涂层定义标准颜色.现在,它是最流行的中欧颜色标准.这些颜色广泛用于建筑学,建筑物,工业和道路安全. 图表中的RAL颜色尽可能 ...
- AppleHDA 10.9.3 disassm 1
1.通过AppleHDAFunctionGroupFactory::createAppleHDAFunctionGroup(DevIdStruct *)实际创建相应的 AppleHDAFunction ...