【问题标题】:In C# how do i query the list of running services on a windows server?在 C# 中,我如何查询 Windows 服务器上正在运行的服务列表?
【发布时间】:2009-05-09 02:22:55
【问题描述】:

我想查询以特定用户身份在远程计算机上运行的服务列表,然后检查每个服务的运行状况。我正在构建一个自定义控制台。

【问题讨论】:

    标签: c# windows


    【解决方案1】:

    ServiceController.GetServices("machineName") 返回特定机器的 ServiceController 对象数组。

    这个:

    namespace AtYourService
    {
        using System;
        using System.ServiceProcess;
    
        class Program
        {
            static void Main(string[] args)
            {
                ServiceController[] services = ServiceController.GetServices();
    
                foreach (ServiceController service in services)
                {
                    Console.WriteLine(
                        "The {0} service is currently {1}.",
                        service.DisplayName,
                        service.Status);
                }
    
                Console.Read();
            }
        }
    }
    

    产生:

    The Application Experience service is currently Running.
    
    The Andrea ST Filters Service service is currently Running.
    
    The Application Layer Gateway Service service is currently Stopped.
    
    The Application Information service is currently Running.
    
    etc...
    

    当然,我是用无参版本在我的机器上获取服务的。

    【讨论】:

      【解决方案2】:

      要使用 ServiceController 方法,我将查看在上一个问题中实现的模拟解决方案:.Net 2.0 ServiceController.GetServices()

      FWIW,这是带有显式主机、用户名、密码的 C#/WMI 方式:

      using System.Management;
      
      static void EnumServices(string host, string username, string password)
      {
          string ns = @"root\cimv2";
          string query = "select * from Win32_Service";
      
          ConnectionOptions options = new ConnectionOptions();
          if (!string.IsNullOrEmpty(username))
          {
              options.Username = username;
              options.Password = password;
          }
      
          ManagementScope scope = 
              new ManagementScope(string.Format(@"\\{0}\{1}", host, ns), options);
          scope.Connect();
      
          ManagementObjectSearcher searcher = 
              new ManagementObjectSearcher(scope, new ObjectQuery(query));
          ManagementObjectCollection retObjectCollection = searcher.Get();
          foreach (ManagementObject mo in retObjectCollection)
          {
              Console.WriteLine(mo.GetText(TextFormat.Mof));
          }
      }
      

      【讨论】:

      • 谢谢。所有答案都帮助我指出了 WMI 和 System.Management。我喜欢这个查询并使用了这个 sn-p。再次感谢。 -k2
      【解决方案3】:

      您可以为此使用 wmi (System.Management)。你也可以使用ServiceController.GetServices()

      【讨论】:

        【解决方案4】:

        这将使用您可以在参数中提及的所需服务名称检查您系统的服务名称

        namespace ServiceName
        {
        using System;
        using System.ServiceProcess;
        
        class Service
        {
        
        public static bool IsServiceInstalled(string serviceName)
        {
        
         ServiceController[] services = ServiceController.GetServices();
        
         foreach (ServiceController service in services)
         {
           if (service.ServiceName == serviceName)
             return true;
         }
         return false;
           }
         }
         }
        

        【讨论】:

          猜你喜欢
          • 2019-10-07
          • 1970-01-01
          • 2019-07-24
          • 1970-01-01
          • 1970-01-01
          • 2019-08-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多