C# 利用 Windows服务模板 创建、安装与卸载Windows服务

什么是Windows服务?
Windows服务,是在Windows操作系统下能够长时间运行的可执行应用程序。
它们在计算机启动后用户登录前就执行,启动方式分自动和手动,可以重启或者暂停,且没有任何用户界面。
Windows服务典型的应用是缓存、异步支付订单...

C#如何创建Windows服务?

利用Windows服务模板

新建项目

单击右键,选择添加安装程序

添加成功后效果如下

分别配置serviceInstaller1、serviceProcessInstaller1的属性

编写服务主体逻辑代码,比如在文本文件中写入一句话

 1 using System;
 2 using System.IO;
 3 using System.ServiceProcess;
 4 using System.Configuration;
 5 
 6 namespace MyWindowsService
 7 {
 8     public partial class Service1 : ServiceBase
 9     {
10         public Service1()
11         {
12             InitializeComponent();
13         }
14 
15         /// <summary>
16         /// 启动服务时执行
17         /// </summary>
18         /// <param name="args"></param>
19         protected override void OnStart(string[] args)
20         {
21             FileStream fs = new FileStream(ConfigurationManager.AppSettings["LogFile"].ToString(), FileMode.OpenOrCreate, FileAccess.Write);
22             StreamWriter sw = new StreamWriter(fs);
23             sw.BaseStream.Seek(0, SeekOrigin.End);
24             sw.WriteLine(string.Format("Windows Service Start At {0} \n", DateTime.Now.ToString()));
25             sw.Flush();
26             sw.Close();
27             fs.Close();
28         }
29 
30         /// <summary>
31         /// 停止服务时执行
32         /// </summary>
33         protected override void OnStop()
34         {
35             FileStream fs = new FileStream(ConfigurationManager.AppSettings["LogFile"].ToString(), FileMode.OpenOrCreate, FileAccess.Write);
36             StreamWriter sw = new StreamWriter(fs);
37             sw.BaseStream.Seek(0, SeekOrigin.End);
38             sw.WriteLine(string.Format("Windows Service Stop At {0} \n", DateTime.Now.ToString()));
39             sw.Flush();
40             sw.Close();
41             fs.Close();
42         }
43     }
44 }
View Code

安装Windows服务

1、以管理员身份运行命令行工具

2、进入InstallUtil.exe文件夹
cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 或 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

3、安装服务,如果路径带空格,用双引号包起来
InstallUtil.exe "F:\maiaimei\WindowsService\Demo\MyWindowsService.exe"

卸载Windows服务

1、以管理员身份运行命令行工具

2、进入InstallUtil.exe文件夹
cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 或 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

3、卸载服务,如果路径带空格,用双引号包起来
InstallUtil.exe /u "F:\maiaimei\WindowsService\Demo\MyWindowsService.exe"

这里强调的是以管理员身份运行命令行工具,否则有可能报错

正在安装程序集“F:\maiaimei\WindowsService\Demo\MyWindowsService.exe”。
受影响的参数是:
   logtoconsole = 
   logfile = F:\maiaimei\WindowsService\Demo\MyWindowsService.InstallLog
   assemblypath = F:\maiaimei\WindowsService\Demo\MyWindowsService.exe
正在安装服务 MyWindowsService...
正在日志 Application 中创建 EventLog 源 MyWindowsService...
正在回滚程序集“F:\maiaimei\WindowsService\Demo\MyWindowsService.exe”。
受影响的参数是:
   logtoconsole = 
   logfile = F:\maiaimei\WindowsService\Demo\MyWindowsService.InstallLog
   assemblypath = F:\maiaimei\WindowsService\Demo\MyWindowsService.exe
正在将事件日志还原到源 MyWindowsService 的前一状态。
在 System.Diagnostics.EventLogInstaller 安装程序的“回滚”阶段发生异常。
System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志。  不可访问的日志: Security。
在安装的“回滚”阶段发生异常。将忽略该异常并继续回滚。但是,在完成回滚后计算机可能无法完全还原到它的初始状态。

源码地址:https://github.com/maiaimei/WindowsService

posted @ 2017-08-15 10:07  初冬十月  Views(3271)  Comments(0Edit  收藏  举报