C# Windows Service 基础
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逻辑
添加如下代码:
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
%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)安装脚本说明
第二行为启动服务。
第三行为设置服务为自动运行。
这2行视服务形式自行选择。
4)脚本调试
如果需要查看脚本运行状况,在脚本最后一行加入pause
五、在C#中对服务进行控制
0)配置目录结构
简历一个新WPF项目,叫WindowsServiceTestUI,添加对System.ServiceProcess的引用。
在WindowsServiceTestUI的bin\Debug目录下建立Service目录。
将WindowsServiceTest的生成目录设置为上面创建的Service目录。
生成后目录结构如下图
1)安装
安装时会产生目录问题,所以安装代码如下:
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)卸载
卸载时也会产生目录问题,所以卸载代码如下:
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)启动
代码如下:
using System.ServiceProcess; ServiceController serviceController = new ServiceController("ServiceTest");
serviceController.Start();
4)停止
ServiceController serviceController = new ServiceController("ServiceTest");
if (serviceController.CanStop)
serviceController.Stop();
5)暂停/继续
ServiceController serviceController = new ServiceController("ServiceTest");
if (serviceController.CanPauseAndContinue)
{
if (serviceController.Status == ServiceControllerStatus.Running)
serviceController.Pause();
else if (serviceController.Status == ServiceControllerStatus.Paused)
serviceController.Continue();
}
6)检查状态
ServiceController serviceController = new ServiceController("ServiceTest");
string Status = serviceController.Status.ToString();
六、调试Windows Service
1)安装并运行服务
2)附加进程
3)在代码中加入断点进行调试
七、总结
本文对Windows service的上述配置都未做详细解释,但是按上述步骤就可以制作可运行的Windows Service,从而达到了工作的需求。
C# Windows Service 基础的更多相关文章
- C#创建Windows Service(Windows 服务)基础教程
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- Windows批处理以服务的方式启动解决思路(ShadowsockR注册成Windows Service)
我以ShadowsockR的server启动来解释: 由于这东西是python,如果要启动,可以写一个批处理(python.exe server.py)来启动,但是我观察发现启动的时候是附带pytho ...
- C# Windows Service服务的创建和调试
前言 关于Windows服务创建和调试的文章在网络上的很多文章里面都有,直接拿过来贴在这里也不过仅仅是个记录,不会让人加深印象.所以本着能够更深刻了解服务项目的创建和调试过程及方法的目的,有了这篇记录 ...
- C# Windows Service调用IBM Lotus Notes发送邮件
近日研究了下IBM Lotus Mail,这货果然是麻烦,由于公司策略,没有开放smtp,很多系统邮件都没有办法发送,于是入手google学习Lotus Mail,想做成Windows服务,提供wcf ...
- C#中级-Windows Service程序安装注意事项
一.前言 这周除了改写一些识别算法外,继续我的Socket服务编写.服务器端的Socket服务是以Windows Service的形式运行的. 在我完成Windows Service编写后,启动服务时 ...
- windows service创建使用整合
C#创建Windows Service(Windows 服务)基础教程 C#winform windows服务程序创建与安装 C#实现WinForm随WINDOWS服务一起启动
- C# 通过 Quartz .NET 实现Timer Job并将其注册成为Windows Service
之前的一篇文章讲述了如何通过 Quartz .NET 实现 Timer Job (http://www.cnblogs.com/mingmingruyuedlut/p/8037263.html) 在此 ...
- C#创建一个Windows Service
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- 震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……
过场CG: 接到公司领导的文件指示,“小熊”需要在6月底去海外执行一个行动代号为[定时任务]的营救计划,这个计划关系到公司某个项目的生死(数据安全漏洞),作战部拟定两个作战方案: 方案一:使用务定 ...
随机推荐
- 更改ssh,ftp默认端口
1. 更改ssh端口 放置升级openssh之后做此步骤 配置文件/etc/ssh/sshd_config 注释掉Subsystem sftp /usr/libexec/openss ...
- Resnet-34框架
import torch import torch.nn as nn import torch.nn.functional as F class ResidualBlock(nn.Module): ' ...
- codeforces 343d
题意:一棵树结构上有水,往一个节点加水,那么所有的子节点都会有水,或者排干一个节点的水,那么它的上面的节点都会没水. 用dfs序,数组记录区间内全部有水为1,区间内有没水的点就为0. 倒水:区间更新, ...
- css实现响应式布局的相关内容
所以我就在做自适应的时候查了一些资料 首先我发现一个问题:有响应式布局和自适应布局两种布局效果 简单来说,响应式布局就是不同的设备无论大小 布局都自动调整大小 页面布局都一样 可以保证无论什么设备 用 ...
- MVC 简介
是AOP (Aspect-Oriented Programming.面向侧 面的程序设计或面向方面的程序设计,是对面向对象程序设计的改进和扩展)内的概念 当 一 件事被细分为各个单元后,各个单元的复 ...
- Jquery("#form_content").validationEngine()失效原因分析
使用validationEngine()函数对表单进行各种校验,由于多个页面都引用了相关js文件,后面子页面的validationEngine()始终不生效:....: 测试后发现重复引用了JQuer ...
- SocketServer模块,hmac模块验证client合法性
hmac模块: 1.模块初识: import hmac # h = hmac.new() #括号里要给它连个bytes类型,一个是自定义的secret_key,一个是你想进行加密的bytes # 密文 ...
- keepalived高可用集群。
keepalived故障切换转移原理1vrrp协议:(vritual router redundancy protocol)虚拟路由冗余协议,2故障转移.keepalived三大功能1实现物理高可用, ...
- 羽翼metasploit第一,二季学习笔记
-----------------第一季-------------------- 启动Metasploit:msfconsole 升级和更新:./msfupdate 直接退出:exit 退回上一级:q ...
- Saiku更改源代码实现默认查询一天的数据(十)
Saiku通过更改源代码实现默认查询前一天数据 saiku在本地进行的编译的方式前面已有教程说明,接下来就是更改原代码了 (从网上学得教程,目前只了解到获取最新一天的数据信息) 参考博客地址: htt ...