美文网首页C#dotNET.NET
C#操作Windows服务的一些方法

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

作者: 张中华 | 来源:发表于2017-11-12 22:43 被阅读86次

    学习网址: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; }
            }
    

    测试:


    查看服务

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



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

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

     /// <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();
            }
    
    测试关闭服务

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

    /// <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();
            }
    

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


    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();
            }
    

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


    测试结果

    好了,练习到此结束。

    相关文章

      网友评论

        本文标题:C#操作Windows服务的一些方法

        本文链接:https://www.haomeiwen.com/subject/gxoomxtx.html