健康一贴灵,专注医药行业管理信息化

c# 如何验证Windows服务是否正在运行

0判断服务状态
复制代码
using System.ServiceProcess; 

// 

ServiceController sc; 
try 
{ 
    sc = new ServiceController(SERVICE_NAME); 
} 
catch(ArgumentException) 
{ 
    return "Invalid service name."; // Note that just because a name is valid does not mean the service exists. 
} 

using(sc) 
{ 
    ServiceControllerStatus status; 
    try 
    { 
     sc.Refresh(); // calling sc.Refresh() is unnecessary on the first use of `Status` but if you keep the ServiceController in-memory then be sure to call this if you're using it periodically. 
     status = sc.Status; 
    } 
    catch(Win32Exception ex) 
    { 
     // A Win32Exception will be raised if the service-name does not exist or the running process has insufficient permissions to query service status. 
     // See Win32 QueryServiceStatus()'s documentation. 
     return "Error: " + ex.Message; 
    } 

    switch(status) 
    { 
     case ServiceControllerStatus.Running: 
      return "Running"; 
     case ServiceControllerStatus.Stopped: 
      return "Stopped"; 
     case ServiceControllerStatus.Paused: 
      return "Paused"; 
     case ServiceControllerStatus.StopPending: 
      return "Stopping"; 
     case ServiceControllerStatus.StartPending: 
      return "Starting"; 
     default: 
      return "Status Changing"; 
    } 
} 
复制代码

 



1. 操作WINDOW服务需要在C#里引入一个类库:System.ServiceProcess
复制代码
        //监测服务是否启动
private bool ServicesExists(string serviceName)
        {
            bool isbn = false;
            //获取所有服务
            ServiceController[] services = ServiceController.GetServices();
            try
            {
                foreach (ServiceController service in services)
                {
                    if (service.ServiceName.ToUpper() == serviceName.ToUpper())
                    {
                        isbn = true;
                        break;
                    }
                }
                return isbn;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //启动服务
        private bool ServiceStar(string serviceName)
        {
            bool isbn = false;

            try
            {
                if (ServicesExists(serviceName))
                {
                    ServiceController star_service = new ServiceController(serviceName);
                    if (star_service.Status != ServiceControllerStatus.Running &&
                    star_service.Status != ServiceControllerStatus.StartPending)
                    {
                        star_service.Start();

                        for (int i = 0; i < 60; i++)
                        {
                            star_service.Refresh();
                            System.Threading.Thread.Sleep(1000);
                            if (star_service.Status == ServiceControllerStatus.Running)
                            {
                                isbn = true;
                                break;
                            }
                            if (i == 59)
                            {
                                isbn = false;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return isbn;
        }

        //停止服务
        private bool ServiceStop(string serviceName)
        {
            bool isbn = false;

            try
            {
                if (ServicesExists(serviceName))
                {
                    ServiceController star_service = new ServiceController(serviceName);
                    if (star_service.Status == ServiceControllerStatus.Running)
                    {
                        star_service.Stop();

                        for (int i = 0; i < 60; i++)
                        {
                            star_service.Refresh();
                            System.Threading.Thread.Sleep(1000);
                            if (star_service.Status == ServiceControllerStatus.Stopped)
                            {
                                isbn = true;
                                break;
                            }
                            if (i == 59)
                            {
                                isbn = false;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return isbn;
        }
复制代码
复制代码
// 判断服务状态
try
{ using(ServiceController sc = new ServiceController(SERVICE_NAME)) { return sc.Status == ServiceControllerStatus.Running; } } catch(ArgumentException) { return false; } catch(Win32Exception) { return false; }
复制代码

 

posted @   一贴灵  阅读(1771)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2021-08-30 SAP 审计日志查看 SM20
2021-08-30 SAP 用户监控管理
2021-08-30 SAP通过事务代码查询菜单位置
2021-08-30 SAP 上线导入数据之 物料标准价-MR21
2017-08-30 转载:SQL 字符串操作函数
学以致用,效率第一
点击右上角即可分享
微信分享提示