C#操作Windows服务的一些方法

学习网址:http://blog.csdn.net/mss359681091/article/details/51073615

主要方法:
启动某个服务
停止某个服务
判断是否安装了某个服务
判断某个服务是否启动

在操作windows服务之前,先添加引用

System.ServiceProcess
1.判断是否安装某服务
/// <summary>
        /// 判断是否安装了某个服务
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public static bool ISWindowsServiceInstalled(string serviceName)
        {
            try
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController service in services)
                {
                    if (service.ServiceName == serviceName)
                    {
                        return true;
                    }
                }
                return false;
            }

            catch

            { return false; }
        }

测试:


2789632-ef025fe630825c63.png
查看服务

此时,我们可以看到MySql是存在的,也是开启的……此处只测试开启的服务,关闭的服务应该是相同的道理,偷个懒,就不去测试了。


2789632-13df60328b5b0e3c.png

在主函数里面添加使用,测试结果
static void Main(string[] args)
        {
            bool MySqlIsExit = ControlerHelper.ISWindowsServiceInstalled("MySQL");
            Console.WriteLine(MySqlIsExit.ToString());
            Console.ReadKey();
        }
2789632-db049e6ca3288241.png
测试结果

此时的服务是开启的,我们先看一下,如何关闭这个服务……

 /// <summary>
        /// 停止某个服务
        /// </summary>
        /// <param name="serviceName"></param>
        public static void StopService(string serviceName)
        {
            try
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController service in services)
                {
                    if (service.ServiceName == serviceName)
                    {
                        service.Stop();
                        service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                    }
                }
            }
            catch { }
        }

在主方法里使用测试:

 static void Main(string[] args)
        {
            bool MySqlIsExit = ControlerHelper.ISWindowsServiceInstalled("MySQL");
            Console.WriteLine(MySqlIsExit.ToString());
            ControlerHelper.StopService("MySQL");
            Console.ReadKey();
        }
2789632-a2a84fffe7bd814e.png
测试关闭服务

当服务关闭时,如果再次执行关闭操作也并不会报错!
此时的服务是关闭的,那接下来看看这个方法,判断服务是否开启:

/// <summary>
        /// 判断某个服务是否启动
        /// </summary>
        /// <param name="serviceName"></param>
        public static bool ISStart(string serviceName)
        {
            bool result = true;
            try
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController service in services)
                {
                    if (service.ServiceName == serviceName)
                    {
                        if ((service.Status == ServiceControllerStatus.Stopped)
                            || (service.Status == ServiceControllerStatus.StopPending))
                        {
                            result = false;
                        }
                    }
                }
            }
            catch { }
            return result;
        }

在主方法里使用测试:

static void Main(string[] args)
        {
            bool MySqlIsExit = ControlerHelper.ISWindowsServiceInstalled("MySQL");
            Console.WriteLine(MySqlIsExit.ToString());
            ControlerHelper.StopService("MySQL");
            bool isStart = ControlerHelper.ISStart("MySQL");
            Console.WriteLine("服务是否启动:" + isStart);
            Console.ReadKey();
        }

可见此时的服务确实是关闭的。


2789632-c6c206a2bdb1c1b5.png
image.png

最后我们再写一个启动服务的方法,进行测试:

 /// <summary>
        /// 启动某个服务
        /// </summary>
        /// <param name="serviceName"></param>

        public static void StartService(string serviceName)
        {
            try
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController service in services)
                {
                    if (service.ServiceName == serviceName)
                    {
                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                    }
                }
            }
            catch { }
        }

在主方法里面把该服务启动,然后再进行判断该服务是否启动。

static void Main(string[] args)
        {
            bool MySqlIsExit = ControlerHelper.ISWindowsServiceInstalled("MySQL");
            Console.WriteLine(MySqlIsExit.ToString());
            ControlerHelper.StopService("MySQL");
            bool isStart = ControlerHelper.ISStart("MySQL");
            Console.WriteLine("服务是否启动:" + isStart);
            ControlerHelper.StartService("MySQL");
            bool isStartAgain = ControlerHelper.ISStart("MySQL");
            Console.WriteLine("服务是否启动:" + isStartAgain);
            Console.ReadKey();
        }

此处开启服务需要一点时间……所以在起初判断服务开启和后来的判断有一点停顿时间。


2789632-ea0338c56f8fece7.png
测试结果

好了,练习到此结束。

2789632-3b18269684ea9294.png
公众号.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值