In C# how do i query the list of running services on a windows server?

前端 未结 4 1536
我寻月下人不归
我寻月下人不归 2020-12-14 01:45

I want to query for a list of services running as a specific user on a remote machine and then check the health of each. I\'m building a custom console.

相关标签:
4条回答
  • 2020-12-14 01:53

    To use the ServiceController method I'd check out the solution with impersonation implemented in this previous question: .Net 2.0 ServiceController.GetServices()

    FWIW, here's C#/WMI way with explicit host, username, password:

    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));
        }
    }
    
    0 讨论(0)
  • 2020-12-14 02:00

    ServiceController.GetServices("machineName") returns an array of ServiceController objects for a particular machine.

    This:

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

    produces:

    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...
    

    Of course, I used the parameterless version to get the services on my machine.

    0 讨论(0)
  • 2020-12-14 02:10

    You can use wmi for this (System.Management). You can also use ServiceController.GetServices().

    0 讨论(0)
  • 2020-12-14 02:15

    This will check you system's service name with your desired service name which you can mention on parameter

    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;
       }
     }
     }
    
    0 讨论(0)
提交回复
热议问题