This documentation is archived and is not being maintained.

Creating ServiceController Component Instances

You can use the ServiceController component to connect to and control the behavior of existing services. When you create an instance of the ServiceController class, you set its properties so that it interacts with a specific Windows service. You can then use the class to start, stop, and otherwise manipulate the service.

You will most likely use a ServiceController in an administrative capacity. For example, you might create a Windows or ASP .NET Web application that sends custom commands to a service via a ServiceController component instance. This would be useful because the Services Control Manager does not support custom commands.

There are several ways you can create an instance of the ServiceController component:

  • You can drag an instance of the ServiceController component from the Components tab of the Toolbox to a form or other designer.
  • You can add a ServiceController component to your designer from Server Explorer
  • You can create an instance of the ServiceController class in code.

After you create an instance of ServiceController, you must set two properties on it to identify the service with which it interacts: the machine name and the name of the service you want to control.

Note   By default, MachineName is set to the local computer, so you do not need to change it unless you want to set the component instance to point to another computer.

To create and configure a ServiceController component from Server Explorer

  1. In Server Explorer, add the server you want if it is not already listed. For more information, see Accessing and Initializing Server Explorer.
    Note   The Servers node of Server Explorer is not available in the Standard Edition of Visual Basic and Visual C# .NET. For more information, see Visual Basic Standard Edition Features or Visual C# Standard Edition Features.
  2. Expand the Services node, and then locate the service on which you want to perform administrative tasks.
  3. Right-click the name of the service, and click Add to Designer.

    A ServiceController component appears in your project, configured to interact with the selected service.

To create and configure a ServiceController component from the Toolbox

  1. Access the Components tab of the Toolbox.
  2. Select the ServiceController icon and drag it to the designer surface for your form or component.
  3. Set the following properties.
    MachineNameThe name of the computer on which the service exists, or "." for the local computer.
    ServiceNameThe name of the service with which you want to interact.
    Tip   You can use Server Explorer to see what the ServiceName should be set to for any given service. Select the service in which you're interested in Server Explorer, and the ServiceName for it appears in the Properties window.

To programmatically create and configure a ServiceController component

  1. Create an instance of the ServiceController class in your code.
  2. Set the MachineName and ServiceName properties to indicate the service you want to control.

    The following example shows how to create a ServiceController component that interacts with the IIS Admin service on the local computer. The component then queries the service associated with the controller to determine if it can accept Stop commands, and if so, issues the command. This example is part of a Windows project in which a form with various buttons is used to start, stop, and otherwise manipulate the service. Each time a button is clicked, the condition is evaluated and status information is displayed in a label control.

    Note   Before these samples will run, you must add a reference to System and System.ServiceProcess DLLs and include an Imports or Using statement for the both namespaces.
    ' Visual Basic
    Sub Main()
       Dim myController As _
          New System.ServiceProcess.ServiceController("IISAdmin")
       If myController.CanStop Then
          Debug.WriteLine(myController.ServiceName & " can be stopped.")
          Debug.WriteLine(myController.ServiceName & " cannot stop.")
       End If
    End Sub
    // C#
    public static void Main(string[] args)
       System.ServiceProcess.ServiceController myController = 
          new System.ServiceProcess.ServiceController("IISAdmin");
       if (myController.CanStop)
             myController.DisplayName + "  can be stopped.");
             myController.DisplayName + "  cannot stop.");   

See Also

Introduction to Communicating with Existing Services | Performing Administrative Tasks on Services | Retrieving Lists of Services