How to: Create 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 through 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 computer 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.
Note
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.
To create and configure a ServiceController component from Server Explorer
In Server Explorer, add the server you want if it is not already listed. For more information, see How to: Access and Initialize Server Explorer/Database Explorer.
Expand the Services node, and then locate the service on which you want to perform administrative tasks.
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
Access the Components tab of the Toolbox.
Select the ServiceController icon and drag it to the designer surface for your form or component.
Set the following properties.
Property
Setting
MachineName
The name of the computer on which the service exists, or "." for the local computer.
ServiceName
The 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
Create an instance of the ServiceController class in your code.
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.ServiceProcess.dll and include an Imports or using statement for the System.ServiceProcess namespace.
Sub Main() Dim myController As _ New System.ServiceProcess.ServiceController("IISAdmin") If myController.CanStop Then Debug.WriteLine(myController.ServiceName & " can be stopped.") Else Debug.WriteLine(myController.ServiceName & " cannot stop.") End If End Sub
public static void Main(string[] args) { System.ServiceProcess.ServiceController myController = new System.ServiceProcess.ServiceController("IISAdmin"); if (myController.CanStop) { System.Diagnostics.Debug.WriteLine( myController.DisplayName + " can be stopped."); } else { System.Diagnostics.Debug.WriteLine( myController.DisplayName + " cannot stop."); } }
See Also
Tasks
How to: Perform Administrative Tasks on Services
How to: Retrieve Lists of Services