Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

ServiceController.GetServices Method ()

Retrieves all the services on the local computer, except for the device driver services.

Namespace: System.ServiceProcess
Assembly: System.ServiceProcess (in system.serviceprocess.dll)

public static ServiceController[] GetServices ()
public static ServiceController[] GetServices ()
public static function GetServices () : ServiceController[]

Return Value

An array of type ServiceController in which each element is associated with a service on the local computer.
Exception typeCondition

Win32Exception

An error occurred when accessing a system API.

GetServices returns only the non-device driver services and the services that are not drivers from the local computer. To retrieve device driver services, call the GetDevices method. Together, the two methods provide access to all the services on a computer.

The following example uses the ServiceController class to display the services that are running on the local computer.

ServiceController[] scServices;
scServices = ServiceController.GetServices();

// Display the list of services currently running on this computer.

Console.WriteLine("Services running on the local computer:");
foreach (ServiceController scTemp in scServices)
{
   if (scTemp.Status == ServiceControllerStatus.Running)
   {
      // Write the service name and the display name
      // for each running service.
      Console.WriteLine();
      Console.WriteLine("  Service :        {0}", scTemp.ServiceName);
      Console.WriteLine("    Display name:    {0}", scTemp.DisplayName);

      // Query WMI for additional information about this service.
      // Display the start name (LocalSytem, etc) and the service
      // description.
      ManagementObject wmiService;
      wmiService = new ManagementObject("Win32_Service.Name='" + scTemp.ServiceName + "'");
      wmiService.Get();
      Console.WriteLine("    Start name:      {0}", wmiService["StartName"]);
      Console.WriteLine("    Description:     {0}", wmiService["Description"]);
   }
}

ServiceController scServices[];
scServices = ServiceController.GetServices();
// Display the list of services currently running on this computer.
Console.WriteLine("Services running on the local computer:");
ServiceController scTemp = null;
for (int iCtr = 0; iCtr < scServices.get_Length(); iCtr++) {
    scTemp = scServices[iCtr];
    if (scTemp.get_Status().Equals(ServiceControllerStatus.Running)) {
        // Write the service name and the display name
        // for each running service.
        Console.WriteLine();
        Console.WriteLine("  Service :        {0}", 
            scTemp.get_ServiceName());
        Console.WriteLine("    Display name:    {0}", 
            scTemp.get_DisplayName());
        // Query WMI for additional information about this service.
        // Display the start name (LocalSytem, etc) and the service
        // description.
        ManagementObject wmiService;
        wmiService = new ManagementObject("Win32_Service.Name='" 
            + scTemp.get_ServiceName() + "'");
        wmiService.Get();
        Console.WriteLine("    Start name:      {0}", 
            wmiService.get_Item("StartName"));
        Console.WriteLine("    Description:     {0}", 
            wmiService.get_Item("Description"));
    }

  • Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see .

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.