Export (0) Print
Expand All

Retrieving Lists of Services

You can use the GetServices method on the ServiceController class to retrieve a list of the services on a particular computer. The GetServices method returns an array of all of a computer's available services, except for those associated with device drivers. You can retrieve the device driver services using the GetDevices method.

For both of these methods, you can either choose a specific computer you're interested in retrieving information about, or you can pass no parameters to retrieve the services on the local computer only.

To retrieve a list of services

  1. Create an array of type ServiceController to contain the results of your query.
  2. Call the appropriate method on the ServiceController class and set the results to the array, using one of the following forms:
    • To retrieve non-device driver services from the local computer, call the GetServices method without any parameters:
      ' Visual Basic
      Dim services() As System.ServiceProcess.ServiceController
      services = System.ServiceProcess.ServiceController.GetServices()
      
      // C#
      System.ServiceProcess.ServiceController[] services;
      services = System.ServiceProcess.ServiceController.GetServices();
      
    • To retrieve non-device driver services from a specific computer, call the GetServices method and specify the computer from which you want to retrieve information as a string parameter:
      ' Visual Basic
      Dim services() As System.ServiceProcess.ServiceController
      services = System.ServiceProcess.ServiceController.GetServices( _
         "machinename")
      
      // C#
      System.ServiceProcess.ServiceController[] services;
      services = System.ServiceProcess.ServiceController.GetServices(
         "machinename");
      
    • To retrieve device driver services from the local computer, call the GetDevices method without any parameters:
      ' Visual Basic
      Dim services() As System.ServiceProcess.ServiceController
      services = System.ServiceProcess.ServiceController.GetDevices()
      
      // C#
      System.ServiceProcess.ServiceController[] services;
      services = System.ServiceProcess.ServiceController.GetDevices();
      
    • To retrieve device driver services from a specific computer, call the GetDevices method and specify the computer from which you want to retrieve information as a string parameter:
      ' Visual Basic
      Dim services() As System.ServiceProcess.ServiceController
      services = System.ServiceProcess.ServiceController.GetDevices( _
         "machinename")
      
      // C#
      System.ServiceProcess.ServiceController[] services;
      services = System.ServiceProcess.ServiceController.GetDevices(
         "machinename");
      

The following code shows how to retrieve a set of services on the local computer and display information about each of them in a listbox. Note that the GetServices function returns an array of ServiceController objects. Each ServiceController returned represents one service on the computer being queried:

' Visual Basic
Sub ListServices()
   Dim ListBox1 As ListBox

   Dim services() As System.ServiceProcess.ServiceController
   Dim i As Integer
   services = System.ServiceProcess.ServiceController.GetServices()
   ListBox1.Items.Clear()
   For i = 0 To services.Length - 1
      ListBox1.Items.Add(services(i).ServiceName)
   Next
End Sub


// C#
private void ListServices() 
{
   ListBox listBox1 = null;

   System.ServiceProcess.ServiceController[] services;
   services = System.ServiceProcess.ServiceController.GetServices();
   listBox1.Items.Clear();
   for (int i = 0; i <  services.Length; i++)
   {
      listBox1.Items.Add(services[i].ServiceName);
   }
}

See Also

Introduction to Communicating with Existing Services | Creating ServiceController Component Instances | Performing Administrative Tasks on Services

Show:
© 2014 Microsoft