[转载]用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();

        }

转载于:https://www.cnblogs.com/roycl/articles/4264780.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值