一、竖立一个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效劳顺序的图文详解的细致内容,更多请关注ki4网别的相干文章!