How to: Retrieve Lists of Services

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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:

      Dim services = System.ServiceProcess.ServiceController.GetServices()
      
              System.ServiceProcess.ServiceController[] services;
              services = System.ServiceProcess.ServiceController.GetServices();
      
      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:

      Dim services = 
        System.ServiceProcess.ServiceController.GetServices("machinename")
      
              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:

      Dim devices = System.ServiceProcess.ServiceController.GetDevices()
      
              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:

      Dim devices = 
          System.ServiceProcess.ServiceController.GetDevices("machinename")
      
              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 list box. Note that the GetServices method returns an array of ServiceController objects. Each ServiceController returned represents one service on the computer being queried:

    Sub ListServices()
        Dim ListBox1 As ListBox
    
        Dim i As Integer
        Dim services = System.ServiceProcess.ServiceController.GetServices()
        ListBox1.Items.Clear()
        For i = 0 To services.Length - 1
            ListBox1.Items.Add(services(i).ServiceName)
        Next
    End Sub
    
        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

Tasks

How to: Create ServiceController Component Instances

How to: Perform Administrative Tasks on Services

Concepts

Introduction to Communicating with Existing Services