C#实操1 创建windows服务,并用winform窗口程序实现安装服务、开启服务、停止服务、卸载服务

C#创建windows服务,并用winform窗口程序实现安装服务、开启服务、停止服务、卸载服务

创建windows服务

创建winform窗口程序

  1. 控制windows服务的的窗口程序,权限要高于一般的winform程序的权限。权限设定如下
    (1) 选中winform项目名称,右键》添加》新建项
    在这里插入图片描述(2)搜索 清单
    在这里插入图片描述(3)打开左侧标记中的文件,在右侧标记,内容修改为:requireAdministrator。
    requestedExecutionLevel:配置权限的地方在这里!默认的是asInvoker,我们修改为requireAdministrator(需要管理员身份!)
    在这里插入图片描述

  2. winform窗口程序,头部引入如下文件报错,出现红色下划线。
    using System.ServiceProcess
    using System.Configurationg.Install
    如图:
    在这里插入图片描述
    产生原因:
    上述俩文件,是.NET4.0版本下的,需要手动点击添加引用。
    解决办法:
    (1)选中winform项目名称,右键》添加》引用,搜索上述两个文件名称。如图:
    在这里插入图片描述(2)
    在这里插入图片描述
    (3)结果如图:
    在这里插入图片描述

  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)
        {

        }
    }
}
  1. 点击运行窗口程序,如果出现如下错误,
    请关闭IDE环境,找到IDE启动程序,右键以管理员身份,重新打开IDE,重新点击运行
    (1)无法打开计算机 上的服务控制管理器。此操作可能需要其他特权
    运行结果如图:
    在这里插入图片描述
    安装后:debug下会多出如下文件
    在这里插入图片描述任务管理器中:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值