Export (0) Print
Expand All

ServiceController.GetServices Method

Retrieves the non-device driver services on a computer, and those that are not drivers.

Overload List

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

[Visual Basic] Overloads Public Shared Function GetServices() As ServiceController()
[C#] public static ServiceController[] GetServices();
[C++] public: static ServiceController* GetServices() [];
[JScript] public static function GetServices() : ServiceController[];

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

[Visual Basic] Overloads Public Shared Function GetServices(String) As ServiceController()
[C#] public static ServiceController[] GetServices(string);
[C++] public: static ServiceController* GetServices(String*) [];
[JScript] public static function GetServices(String) : ServiceController[];

Example

[Visual Basic, C#, C++] The following example uses the ServiceController class to display the services that are running on the local computer.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetServices. For other examples that might be available, see the individual overload topics.
[Visual Basic] 

         Dim scServices() As ServiceController
         scServices = ServiceController.GetServices()
       
         ' Display the list of services currently running on this computer.
         Console.WriteLine("Services running on the local computer:")

         Dim scTemp As ServiceController
         For Each scTemp In  scServices

            If scTemp.Status = ServiceControllerStatus.Running Then
               ' 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.
               Dim wmiService As ManagementObject
               wmiService = New ManagementObject("Win32_Service.Name='" + scTemp.ServiceName + "'")
               wmiService.Get()
               Console.WriteLine("    Start name:      {0}", wmiService("StartName"))
               Console.WriteLine("    Description:     {0}", wmiService("Description"))
            End If

         Next scTemp
      

[C#] 
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"]);
   }
}

[C++] 
ServiceController * scServices[] = ServiceController::GetServices();

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

Console::WriteLine("Services running on the local computer:");
for (int i=0; i<scServices->Length; i++)
{
   ServiceController *scTemp = scServices[i];

   if (scTemp)
   {
      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;
         String *objPath;
   
         objPath = String::Format(S"Win32_Service.Name='{0}'", scTemp->ServiceName);
         wmiService = new ManagementObject(objPath);
         if (wmiService)
         {
            wmiService->Get();

            Object *objStartName = wmiService->get_Item(S"StartName");
            Object *objDescription = wmiService->get_Item(S"Description");
            if (objStartName)
            {
               Console::WriteLine("    Start name:      {0}", objStartName->ToString());
            }
            if (objDescription)
            {
               Console::WriteLine("    Description:     {0}", objDescription->ToString());
            }
         }
      }
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

ServiceController Class | ServiceController Members | System.ServiceProcess Namespace

Show:
© 2015 Microsoft