操纵系统:Windows 10 X64
开发环境:VS2015
编程语言:C#
.NET版本:.NET Framework 4.0
方针平台:X86
二、创建Windows Service
1、新建一个Windows Service,并将项目名称改为“MyWindowsService”,如下图所示:
2、在解决方案资源打点器内将Service1.cs改为MyService1.cs后并点击“检察代码”图标按钮进入代码编纂器界面,如下图所示:
3、在代码编纂器内如入以下代码,如下所示:
using System; using System.ServiceProcess; using System.IO; namespace MyWindowsService { public partial class MyService : ServiceBase { public MyService() { InitializeComponent(); } string filePath = @"D:\MyServiceLog.txt"; protected override void OnStart(string[] args) { using (FileStream stream = new FileStream(filePath,FileMode.Append)) using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine($"{DateTime.Now},处事启动!"); } } protected override void OnStop() { using (FileStream stream = new FileStream(filePath, FileMode.Append)) using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine($"{DateTime.Now},处事遏制!"); } } } }
4、双击项目“MyWindowsService”进入“MyService”设计界面,,在空白位置右击鼠标弹出上下文菜单,选中“添加安置措施”,如下图所示:
5、此时软件会生成两个组件,分袂为“serviceInstaller1”及“serviceProcessInstaller1”,如下图所示:
6、点击“serviceInstaller1”,在“属性”窗体将ServiceName改为MyService,Description改为我的处事,StartType连结为Manual,如下图所示:
7、点击“serviceProcessInstaller1”,在“属性”窗体将Account改为LocalSystem(处事属性系统级别),如下图所示:
8、鼠标右键点击项目“MyWindowsService”,在弹出的上下文菜单中选择“生成”按钮,如下图所示:
9、至此,Windows处事已经创建完毕。
三、创建安置、启动、遏制、卸载处事的Windows窗体
1、在同一个解决方案里新建一个Windows Form项目,并定名为WindowsServiceClient,如下图所示:
2、将该项目设置为启动项目,并在窗体内添加四个按钮,分袂为安置处事、启动处事、遏制处事及卸载处事,如下图所示:
3、按下F7进入代码编纂界面,引用“System.ServiceProcess”及“System.Configuration.Install”,并输入如下代码:
using System; using System.Collections; using System.Windows.Forms; using System.ServiceProcess; using System.Configuration.Install; namespace WindowsServiceClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string serviceFilePath = $"{Application.StartupPath}\\MyWindowsService.exe"; string serviceName = "MyService"; //事件:安置处事 private void button1_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName); this.InstallService(serviceFilePath); } //事件:启动处事 private void button2_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName); } //事件:遏制处事 private void button4_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName); } //事件:卸载处事 private void button3_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) { this.ServiceStop(serviceName); this.UninstallService(serviceFilePath); } } //判断处事是否存在 private bool IsServiceExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController sc in services) { if (sc.ServiceName.ToLower() == serviceName.ToLower()) { return true; } } return false; } //安置处事 private void InstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; IDictionary savedState = new Hashtable(); installer.Install(savedState); installer.Commit(savedState); } } //卸载处事 private void UninstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; installer.Uninstall(null); } } //启动处事 private void ServiceStart(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Stopped) { control.Start(); } } } //遏制处事 private void ServiceStop(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Running) { control.Stop(); } } } } }
4、为了后续调试处事及安置卸载处事的需要,将已生成的MyWindowsService.exe引用到本Windows窗体,如下图所示: