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

测试:


查看服务

此时,我们可以看到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();
        }

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


测试结果

好了,练习到此结束。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,259评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,956评论 25 709
  • 沙漠,沙漠,那突然涌来的黄色的风混着炎日发了疯的味道,使劲地将炙热揉碎在脸上。看不见一丝绿色,除了一望无际的黄色...
    一条秋刀鱼阅读 3,590评论 1 2
  • 量化交易起源于国外,在国外已经至少有长达几十年的发展历程,因此我们先看一下国外比较经典有效的一些策略。 中长线的交...
    鏡澤阅读 11,745评论 1 6
  • 1.小白,解决有没有仓位、仓位上升速度问题。选择高成长区域,低资金高杠杆,快速滚动。大概能冲击1-2kw。 2.中...
    郑州投资俱乐部sk阅读 1,351评论 0 0

友情链接更多精彩内容