C#使用代码安装/启动/停止/卸载Windows服务

重要:必须右键管理员权限运行程序,否则无法正常安装Windows服务

界面:

 

使用ManagedInstallerClass安装和卸载服务,使用ServiceController启动和停止服务,代码:

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

namespace UIWinformTest
{
    public partial class WindowsServiceInstall : Form
    {
        string[] argsInstall = new string[] { Path.Combine(Environment.CurrentDirectory, @"service\WindowsService1.exe") };
        string[] argsUninstall = new string[] { "/u", Path.Combine(Environment.CurrentDirectory, @"service\WindowsService1.exe") };

        ServiceController serviceController;
        static readonly string serviceName = "Service1";

        public WindowsServiceInstall()
        {
            InitializeComponent();
        }

        private void btnInstallService_Click(object sender, EventArgs e)
        {
            try
            {
                ManagedInstallerClass.InstallHelper(argsInstall);
                MessageBox.Show("服务安装成功");
            }
            catch(Exception ex)
            {
                MessageBox.Show("服务安装失败!\r\n" + ex.ToString());
            }
        }

        private void btnUninstallService_Click(object sender, EventArgs e)
        {
            try
            {
                ManagedInstallerClass.InstallHelper(argsUninstall);
                MessageBox.Show("服务卸载成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务卸载失败!\r\n" + ex.ToString());
            }
        }

        private void btnStartService_Click(object sender, EventArgs e)
        {
            try
            {
                serviceController = new ServiceController(serviceName);

                if (serviceController.Status != ServiceControllerStatus.Running && serviceController.Status != ServiceControllerStatus.StartPending)
                {
                    serviceController.Start();
                    serviceController.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30));
                    MessageBox.Show("服务启动成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务启动失败: " + ex.Message);
            }
        }

        private void btnStopService_Click(object sender, EventArgs e)
        {
            try
            {
                serviceController = new ServiceController(serviceName);

                if (serviceController.Status != ServiceControllerStatus.Stopped && serviceController.Status != ServiceControllerStatus.StopPending)
                {
                    serviceController.Stop();
                    serviceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30));
                    MessageBox.Show("服务停止成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务停止失败: " + ex.Message);
            }
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值