kkelly18
kkelly18

Reputation: 518

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

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.

Upvotes: 28

Views: 34961

Answers (4)

Shea
Shea

Reputation: 11243

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

Upvotes: 5

Aaron Daniels
Aaron Daniels

Reputation: 9664

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.

Upvotes: 46

xcud
xcud

Reputation: 14732

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

Upvotes: 24

Mehedi hasan
Mehedi hasan

Reputation: 111

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

Upvotes: 4

Related Questions