用C#代码来安装卸载启动关闭服务

Posted update_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C#代码来安装卸载启动关闭服务相关的知识,希望对你有一定的参考价值。

 /// <summary>
        /// 启动服务
         /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            ServiceController sc = new ServiceController("WindowsService1");
            if (sc.Status.Equals(ServiceControllerStatus.Stopped))
            {
                sc.Start();
            }
        }
        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            ServiceController sc = new ServiceController("MSSQLSERVER");
            if (!sc.Status.Equals(ServiceControllerStatus.Stopped))
            {
                sc.Stop();
            }
        }
        /// <summary>
        /// 安装服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (!isServiceIsExisted("Service1"))
            {                
                string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
                string serviceFileName = location.Substring(0, location.LastIndexOf(\'//\') + 1) + "WindowsService1.exe";

                InstallmyService(null, serviceFileName);
            }
            else
            {
                MessageBox.Show("系统已经安装了此服务!");
            }
        }
        /// <summary>
        /// 卸载服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (isServiceIsExisted("Service1"))
            {
                string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
                string serviceFileName = location.Substring(0, location.LastIndexOf(\'//\') + 1) + "WindowsService1.exe";
                UnInstallmyService(serviceFileName);
            }
            else
            {
                MessageBox.Show("系统不存在此服务,不需要卸载!");
            }
        }


        /// <summary>
        /// 检查服务存在的存在性
        /// </summary>
        /// <param name=" NameService ">服务名</param>
        /// <returns>存在返回 true,否则返回 false;</returns>
        public static bool isServiceIsExisted(string NameService)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController s in services)
            {
                if (s.ServiceName.ToLower() == NameService.ToLower())
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 安装Windows服务
        /// </summary>
        /// <param name="stateSaver">集合</param>
        /// <param name="filepath">程序文件路径</param>
        public static void InstallmyService(IDictionary stateSaver, string filepath)
        {
            AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
            AssemblyInstaller1.UseNewContext = true;
            AssemblyInstaller1.Path = filepath;
            AssemblyInstaller1.Install(stateSaver);
            AssemblyInstaller1.Commit(stateSaver);
            AssemblyInstaller1.Dispose();
        }
        /// <summary>
        /// 卸载Windows服务
        /// </summary>
        /// <param name="filepath">程序文件路径</param>
        public static void UnInstallmyService(string filepath)
        {
            AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
            AssemblyInstaller1.UseNewContext = true;
            AssemblyInstaller1.Path = filepath;
            AssemblyInstaller1.Uninstall(null);
            AssemblyInstaller1.Dispose();
        }

以上是关于用C#代码来安装卸载启动关闭服务的主要内容,如果未能解决你的问题,请参考以下文章

如何自动启动窗口服务

C#中如何动态加载和卸载DLL

C#创建服务及使用程序自动安装服务,.NET创建一个即是可执行程序又是Windows服务的exe

[Ubuntu] 使用dpkg来安装/卸载deb包

怎么样使用yum来安装,卸载jdk

使用yum来安装或卸载CentOS图形界面包