c# 实现windows服务程序的安装、启动、暂停以及卸载 等管理功能

112
补充展位 Pages_Weblog_Get#0
文章摘要
此内容由人工摘要内容,并由AI根据文章内容进行润色
暂无内容

window服务程序的部署相对比较麻烦 我们可以通过程序实现自定义的安装管理程序来执行 以下是c#实现windows服务程序的安装、启动、暂停以及卸载 的功能代码实现

首先需要引用这两个dll类库

using System.Configuration.Install;
using System.ServiceProcess;

以下是封装方法

/// <summary>
    /// 本地服务集中管理
    /// </summary>
    public class ServiceManager
    {
        /// <summary>
        /// 服务主程序的文件地址
        /// </summary>
        public string ServiceFilePath { get; private set; }
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; private set; }

        public ServiceManager(string ServiceFilePath)
        {
            this.ServiceFilePath = ServiceFilePath;
            this.ServiceName = System.IO.Path.GetFileNameWithoutExtension(ServiceFilePath);
        }
 
        /// <summary>
        /// 判断服务是否存在
        /// </summary>
        /// <returns></returns>
        public bool IsExisted
        {
            get
            {
                ServiceController[] services = ServiceController.GetServices();
                return services.Count(e => e.ServiceName.ToLower() == ServiceName.ToLower())>0;
            }
        }

        /// <summary>
        /// 安装服务
        /// </summary>
        public void Install()
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = ServiceFilePath;
                IDictionary savedState = new Hashtable();
                installer.Install(savedState);
                installer.Commit(savedState);
            }
        }

        /// <summary>
        /// 卸载服务
        /// </summary>
        public void Uninstall()
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = ServiceFilePath;
                installer.Uninstall(null);
            }
        }

        /// <summary>
        /// 启动服务
        /// </summary>
        public void Start()
        {
            using (ServiceController control = new ServiceController(ServiceName))
            {
                if (control.Status == ServiceControllerStatus.Stopped)
                {
                    control.Start();
                }
            }
        }

        /// <summary>
        /// 获取服务状态
        /// </summary>
        /// <returns></returns>
        public ServiceControllerStatus Status
        {
            get
            {
                ServiceControllerStatus RunStatus = ServiceControllerStatus.Stopped;
                if (this.IsExisted)
                {
                    using (ServiceController control = new ServiceController(ServiceName))
                    {
                        RunStatus = control.Status;
                    }
                }
                return RunStatus;
            }
        }



        /// <summary>
        /// 获取服务名称
        /// </summary>
        /// <returns></returns>
        public string DisplayName
        {
            get
            {
                string displayName="??" ;
                if (this.IsExisted)
                {
                    using (ServiceController control = new ServiceController(ServiceName))
                    {
                        displayName = control.DisplayName;
                    }
                }
                return displayName;
            }
        }



        /// <summary>
        /// 停止服务
        /// </summary>
        public void Stop()
        {
            using (ServiceController control = new ServiceController(ServiceName))
            {
                if (control.Status == ServiceControllerStatus.Running)
                {
                    control.Stop();
                }
            }
        }

        public static void Uninstall(string xxxx)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = xxxx;
                installer.Uninstall(null);
            }
        }
    }
补充展位
Pages_Weblog_Get#5f85cfa4-56e8-44a8-a27c-ad4a00a10349
补充展位 Pages_Weblog_Get#1
补充展位 Pages_Weblog_Get#2
专题推荐
暂无内容
补充展位 Pages_Weblog_Get#3