还在苦苦敲代码开发APP?你out啦! 试试积木搭建APP吧~

C# 通过代码安装、卸载、启动、停止服务

来源:清泛原创     2016-06-27 15:42:44    人气:     我有话说( 0 人参与)

直接贴代码,亲测可用:#region Windows服务控制区 #region 安装服务 private void InstallService(str...

直接贴代码,亲测可用:
#region Windows服务控制区

        #region 安装服务
        private void InstallService(string filepath, string serviceName)
        {
            try
            {
                IDictionary _SavedState = new Hashtable();
                ServiceController service = new ServiceController(serviceName);
                string dispName = string.Empty;
                if (!ServiceIsExisted(serviceName, ref dispName))
                {
                    // Install Service
                    AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                    myAssemblyInstaller.UseNewContext = true;
                    myAssemblyInstaller.Path = filepath;
                    myAssemblyInstaller.Install(_SavedState);
                    myAssemblyInstaller.Commit(_SavedState);
                    myAssemblyInstaller.Dispose();
                    // --Start Service
                    // service.Start();
                }
                else
                {
                    if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                    {
                        service.Start();
                    }
                }

                statusMsg.Text = "服务安装成功!";
            }
            catch (Exception ex)
            {
                statusMsg.Text = "安装服务失败!" + ex.Message;
            }
        }
        #endregion

        #region 卸载windows服务
        private void UnInstallService(string filepath, string serviceName)
        {
            try
            {
                string dispName = string.Empty;
                if (ServiceIsExisted(serviceName, ref dispName))
                {
                    // UnInstall Service  
                    AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                    myAssemblyInstaller.UseNewContext = true;
                    myAssemblyInstaller.Path = filepath;
                    myAssemblyInstaller.Uninstall(null);
                    myAssemblyInstaller.Dispose();
                }

                statusMsg.Text = "服务卸载成功!";
            }
            catch (Exception ex)
            {
                statusMsg.Text = "卸载服务失败!" + ex.Message;
            }
        }
        #endregion

        #region 判断window服务是否存在
        private bool ServiceIsExisted(string serviceName, ref string dispName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController s in services)
            {
                if (s.ServiceName == serviceName)
                {
                    dispName = s.DisplayName;
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region 判断window服务是否启动
        /// <summary>  
        /// 判断某个Windows服务是否启动  
        /// </summary>  
        /// <returns></returns>  
        public static bool IsServiceStart(string serviceName)
        {
            ServiceController psc = new ServiceController(serviceName);
            bool bStartStatus = false;
            try
            {
                if (!psc.Status.Equals(ServiceControllerStatus.Stopped))
                {
                    bStartStatus = true;
                }

                return bStartStatus;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 启动服务
        private bool StartService(string serviceName)
        {
            bool flag = true;
            string dispName = string.Empty;
            if (ServiceIsExisted(serviceName, ref dispName))
            {
                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                {
                    service.Start();
                    for (int i = 0; i < 60; i++)
                    {
                        service.Refresh();
                        System.Threading.Thread.Sleep(1000);
                        if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                        {
                            break;
                        }
                        if (i == 59)
                        {
                            flag = false;
                        }
                    }
                }
            }
            return flag;
        }
        #endregion

        #region 停止服务
        private bool StopService(string serviceName)
        {
            bool flag = true;
            string dispName = string.Empty;
            if (ServiceIsExisted(serviceName, ref dispName))
            {
                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                {
                    service.Stop();
                    for (int i = 0; i < 60; i++)
                    {
                        service.Refresh();
                        System.Threading.Thread.Sleep(1000);
                        if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                        {
                            break;
                        }
                        if (i == 59)
                        {
                            flag = false;
                        }
                    }
                }
            }
            return flag;
        }
        #endregion
#endregion
需要添加以下命名空间:
using System.ServiceProcess;
using System.Configuration.Install;

C# 服务管理

注:本文为本站或本站会员原创优质内容,版权属于原作者及清泛网所有,
欢迎转载,转载时须注明版权并添加来源链接,谢谢合作! (编辑:admin)
分享到: