本文共 5679 字,大约阅读时间需要 18 分钟。
作为开发人员,在实际项目中常需要创建并管理Windows服务。以下将详细介绍如何创建一个Windows服务及其对应的安装、启动、停止及卸载功能的Windows窗体。
开发环境的选择对项目的顺利进行至关重要。具体来说:
Service1.cs
更改为MyService1.cs
,然后点击“查看代码”图标进入代码编辑界面。在代码编辑器中输入以下代码:
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}, 服务停止!"); } } }}
双击项目"MyWindowsService",进入服务设计界面。
在空白区域右击,选择"添加安装程序"。
在服务安装程序属性窗口中设置:
设置服务进程属性:
点击项目"MyWindowsService"的右键菜单中的"生成"按钮,即可完成服务的创建。
在窗体中添加四个按钮,分别用于安装、启动、停止和卸载服务,按钮名称可分别为"安装服务"、"启动服务"、"停止服务"和"卸载服务"。
在代码编辑器中引用必要的命名空间:
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 (IsServiceExisted(serviceName)) UninstallService(serviceFilePath); InstallService(serviceFilePath); } // 启动服务 private void button2_Click(object sender, EventArgs e) { if (IsServiceExisted(serviceName)) ServiceStart(serviceName); } // 停止服务 private void button4_Click(object sender, EventArgs e) { if (IsServiceExisted(serviceName)) ServiceStop(serviceName); } // 卸载服务 private void button3_Click(object sender, EventArgs e) { if (IsServiceExisted(serviceName)) { ServiceStop(serviceName); 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(); } } } }}
在项目中引用生成的"MyWindowsService.exe",确保安装、启动、停止和卸载操作正常进行。
为了确保服务安装和操作的权限,需要使用UAC管理员权限:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
替换为<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
。servicess.msc
,打开服务窗口。服务启动和停止操作会将日志记录在D:\MyServiceLog.txt
文件中,便于后续调试和监控。
为了方便调试,按如下步骤进行:
MyService
类中设置断点,如OnStart
和OnStop
方法中的断点。通过以上步骤,可以成功创建并管理Windows服务及其相关功能。
转载地址:http://prtk.baihongyu.com/