Aracılığıyla paylaş


How to: Retrieve 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:

      Dim services() As System.ServiceProcess.ServiceController
      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() As System.ServiceProcess.ServiceController
      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 services() As System.ServiceProcess.ServiceController
      services = 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 services() As System.ServiceProcess.ServiceController
      services = 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 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
    
    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);
            }
        }
    

This code example is also available as a Visual Basic IntelliSense code snippet. In the code snippet picker, it is located in Windows Operating System > Windows Services. For more information, see How to: Insert Snippets Into Your Code (Visual Basic).

See Also

Tasks

How to: Create ServiceController Component Instances

How to: Perform Administrative Tasks on Services

Concepts

Introduction to Communicating with Existing Services