安装与卸载windows服务

https://www.cnblogs.com/james641/p/6307027.html

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string _servicePath = $"{Application.StartupPath}//WindowsService1.exe";
        string _serviceName = "MyService1";
        private void btnInit_Click(object sender, EventArgs e)
        {
            if (this.IsServiceExited(_serviceName))
                this.InstrallService(_servicePath);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (this.IsServiceExited(_serviceName))
                this.ServiceStart(_serviceName);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (this.IsServiceExited(_serviceName))
                this.ServiceStop(_serviceName);
        }

        private void btnUninit_Click(object sender, EventArgs e)
        {
            if (this.IsServiceExited(_serviceName))
                this.UninsreallService(_servicePath);
        }

        //判断服务是否存在
        private bool IsServiceExited(string serviceName)
        {
            ServiceController[] controllers = ServiceController.GetServices();
            foreach (var contrl in controllers)
            {
                if (contrl.ServiceName.ToLower() == serviceName.ToLower())
                    return true;
            }
            return false;
        }

        //卸载服务
        private void UninsreallService(string servicePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = servicePath;
                installer.Uninstall(null);
            }
        }

        //安装服务
        private void InstrallService(string servicePath)
        {
            using (AssemblyInstaller instraller = new AssemblyInstaller())
            {
                instraller.UseNewContext = true;
                instraller.Path = servicePath;
                IDictionary saveState = new Hashtable();
                instraller.Install(saveState);
                instraller.Commit(saveState);
            }
        }

        //启动服务
        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();
                }
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        private string filePath = "D://MyServiceLog.txt";
        public Service1()
        {
            InitializeComponent();

        }

        protected override void OnStart(string[] args)
        {
            using (var fileStream = new FileStream(filePath, FileMode.Append))
            {
                using (StreamWriter sw = new StreamWriter(fileStream))
                {
                    sw.WriteLine($"{DateTime.Now}:服务启动");
                }
            }
        }

        protected override void OnStop()
        {
            using (var fileStream = new FileStream(filePath, FileMode.Append))
            using (StreamWriter sw = new StreamWriter(fileStream))
            {
                sw.WriteLine($"{DateTime.Now}:服务停止");
            }
        }
    }
}

发布了15 篇原创文章 · 获赞 3 · 访问量 9538

猜你喜欢

转载自blog.csdn.net/weixin_38660590/article/details/88560962
今日推荐