转:http://www.cnblogs.com/sorex/archive/2012/05/16/2502001.htmlhtml
Windows Service这一块并不复杂,可是注意事项太多了,网上资料也很凌乱,偶尔本身写也会丢三落四的。因此本文也就产生了,本文不会写复杂的东西,彻底以基础应用的需求来写,因此不会对Windows Service写很深刻。git
本文介绍了如何用C#建立、安装、启动、监控、卸载简单的Windows Service 的内容步骤和注意事项。github
将Service1重命名为你服务名称,这里咱们命名为ServiceTest。shell
以后咱们能够看到上图,自动为咱们建立了ProjectInstaller.cs以及2个安装的组件。ide
右键serviceInsraller1,选择属性,将ServiceName的值改成ServiceTest。spa
右键serviceProcessInsraller1,选择属性,将Account的值改成LocalSystem。3d
右键ServiceTest,选择查看代码。调试
添加以下代码:日志
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."); } } } }
这里咱们的逻辑很简单,启动服务的时候写个日志,关闭的时候再写个日志。htm
在项目中添加2个文件以下(必须是ANSI或者UTF-8无BOM格式):
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe WindowsServiceTest.exe Net Start ServiceTest sc config ServiceTest start= auto
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u WindowsServiceTest.exe
第二行为启动服务。
第三行为设置服务为自动运行。
这2行视服务形式自行选择。
若是须要查看脚本运行情况,在脚本最后一行加入pause
简历一个新WPF项目,叫WindowsServiceTestUI,添加对System.ServiceProcess的引用。
在WindowsServiceTestUI的bin\Debug目录下创建Service目录。
将WindowsServiceTest的生成目录设置为上面建立的Service目录。
生成后目录结构以下图
安装时会产生目录问题,因此安装代码以下:
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;
卸载时也会产生目录问题,因此卸载代码以下:
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;
代码以下:
using System.ServiceProcess; ServiceController serviceController = new ServiceController("ServiceTest"); serviceController.Start();
ServiceController serviceController = new ServiceController("ServiceTest"); if (serviceController.CanStop) serviceController.Stop();
ServiceController serviceController = new ServiceController("ServiceTest"); if (serviceController.CanPauseAndContinue) { if (serviceController.Status == ServiceControllerStatus.Running) serviceController.Pause(); else if (serviceController.Status == ServiceControllerStatus.Paused) serviceController.Continue(); }
ServiceController serviceController = new ServiceController("ServiceTest"); string Status = serviceController.Status.ToString();
本文对Windows service的上述配置都未作详细解释,可是按上述步骤就能够制做可运行的Windows Service,从而达到了工做的需求。