此时软件会生成两个组件,分别为“serviceInstaller1”及“serviceProcessInstaller1”,如下图所示:
首先设置serviceProcessInstaller1 User改为LocalSystem
点击“servicestaller1”,在“属性”窗体将ServiceName改为MyService,Description改为我的服务,StartType保持为Manual,如下图所示:
F7进入代码编辑页面 Service页面 编辑好生成一下
public partial class MyService : ServiceBase
public MyService()
InitializeComponent();
string filePath = @"E:\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},服务启动!");
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += new System.Timers.ElapsedEventHandler(TimedEvent);
timer.Interval = 1000 * 5;
timer.Enabled = true;
private void TimedEvent(object sender, System.Timers.ElapsedEventArgs e)
Task.Run(() =>
GetAllOrder getAllOrder = new GetAllOrder();
getAllOrder.GetAll();
protected override void OnStop()
using (FileStream stream = new FileStream(filePath, FileMode.Append))
using (StreamWriter writer = new StreamWriter(stream))
writer.WriteLine($"{DateTime.Now},服务停止!");
在bin根目录文件夹下会出现 MyWindowsService.exe文件
在MyWindowsService的App.config里设置数据库链接
二、创建安装、启动、停止、卸载服务的Windows窗体
在WinForm应用程序添加新建项 添加应用程序清单文件app.manifest
设置app.manifest 把level="asInvoker"设置成level="requireAdministrator"
F7进入代码编辑页面
public partial class Form1 : Form
public Form1()
InitializeComponent();
string serviceFilePath = $"{AppDomain.CurrentDomain.BaseDirectory}\\MyWindowsService.exe";
string serviceName = "MyService";
private void Install_Click(object sender, EventArgs e)
if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceFilePath);
this.InstallService(serviceFilePath);
private void StartUp_Click(object sender, EventArgs e)
if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
private void StopOff_Click(object sender, EventArgs e)
if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
private void UnInstall_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();
引用我们要执行的服务
使用WIN+R的方式打开运行窗体,并在窗体内输入services.msc后打开服务,如下图所示:
点击窗体内的“安装服务”按钮,将会在服务中出现MyService,如下图所示:
点击“运行服务”按钮,将启动并运行服务,如下所示:
