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.exeNet Start ServiceTestsc 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 ...
随机推荐
- codeforces 21D:Traveling Graph
Description You are given undirected weighted graph. Find the length of the shortest cycle which sta ...
- $(document).click() 在苹果手机上不能正常运行
前几天,一位同事在微信上测试一个JQuery的写的购物车功能,出现了一个很奇怪的现象,在android系统上可以正常绑定,但是在iOS上确不能用,经过排除分析,发现其使用了jquery的$(docum ...
- android下载简单工具类
功能是实现下载文件,图片或MP3等,为了简单起见使用单线程,此代码为MarsAndroid教程的复制品,放在此处,留着参考. 首先是一个得到字节流随后保存到内存卡上的工具类: package com. ...
- poj3744 Scout YYF I
题意:n个地雷(n<=10)在长度10^8的坐标轴上,yyf从横坐标为1的点开始,每一步有p的概率向右跳一格,(1-p)的概率向右跳两格(不会踩到中间一格),如果踩到地雷他就会死.问活下来的概率 ...
- 提高效率的Matlab使用方式
1.花一点时间学习一些提高效率的技巧永远是值得的: 2.总结和记录永远是必要的. Command窗口: Editor窗口: 1.Tab自动补全
- (转载)LCA问题的Tarjan算法
转载自:Click Here LCA问题(Lowest Common Ancestors,最近公共祖先问题),是指给定一棵有根树T,给出若干个查询LCA(u, v)(通常查询数量较大),每次求树T中两 ...
- 网络存储(四)之ISCSI的进阶
前言 上一篇博客中我们讲了如何搭建一个简单的iscsi网络存储系统,这块有个安全问题就是,任何知道target name的客户端都可以随意连接ISCSI服务器.但是很多时候,通过授权认证连接共享磁盘或 ...
- 细说Linux下软件包的安装与管理
一 源码安装方式 由于linux操作系统开放源代码,因而在其上安装的软件大部分也都是开源软件,例如apache.tomcat.php等软件.开源软件基本都提供源码下载,源码安装的方式:源码安 ...
- 安装beautifulsoup4
python scripts下 pip install beautifulsoup4
- Zipf定律
http://www.360doc.com/content/10/0811/00/84590_45147637.shtml 英美在互联网具有绝对霸权 Zipf定律是美国学者G.K.齐普夫提出的.可以表 ...