C#创建windows服务,并用winform窗口程序实现安装服务、开启服务、停止服务、卸载服务
创建windows服务
创建winform窗口程序
-
控制windows服务的的窗口程序,权限要高于一般的winform程序的权限。权限设定如下
(1) 选中winform项目名称,右键》添加》新建项
(2)搜索 清单
(3)打开左侧标记中的文件,在右侧标记,内容修改为:requireAdministrator。
requestedExecutionLevel:配置权限的地方在这里!默认的是asInvoker,我们修改为requireAdministrator(需要管理员身份!)
-
winform窗口程序,头部引入如下文件报错,出现红色下划线。
using System.ServiceProcess
using System.Configurationg.Install
如图:
产生原因:
上述俩文件,是.NET4.0版本下的,需要手动点击添加引用。
解决办法:
(1)选中winform项目名称,右键》添加》引用,搜索上述两个文件名称。如图:
(2)
(3)结果如图:
-
具体代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceProcess;//用来控制服务的启动和停止
using System.Configuration.Install;//用来控制服务的安装与卸载
namespace ServiceClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string serviceFilePath = $"{Application.StartupPath}\\WindowsService1.exe";
string serviceName = "WindowsService1";
//判断服务是否存在
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);
installer.Dispose();
}
}
//启动服务
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();
}
}
}
//安装服务
private void button1_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) {
DialogResult dr = MessageBox.Show("服务已存在,是否卸载后重新安装?");
if (dr==DialogResult.Yes) {
this.UninstallService(serviceName);
}
}
this.InstallService(serviceFilePath);
}
//启动服务
private void button2_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) {
this.ServiceStart(serviceName);
}
}
//停止服务
private void button3_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) {
this.ServiceStop(serviceName);
}
}
//卸载服务
private void button4_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName))
{
this.ServiceStop(serviceName);
this.UninstallService(serviceFilePath);
}
else {
DialogResult dr = MessageBox.Show("服务不存在,卸载失败!");
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
- 点击运行窗口程序,如果出现如下错误,
请关闭IDE环境,找到IDE启动程序,右键以管理员身份,重新打开IDE,重新点击运行
(1)无法打开计算机 上的服务控制管理器。此操作可能需要其他特权
运行结果如图:
安装后:debug下会多出如下文件
任务管理器中: