TechNet
Export (0) Print
Expand All

ServiceController Class

 

Represents a Windows service and allows you to connect to a running or stopped service, manipulate it, or get information about it.

Namespace:   System.ServiceProcess
Assembly:  System.ServiceProcess (in System.ServiceProcess.dll)

System::Object
  System::MarshalByRefObject
    System.ComponentModel::Component
      System.ServiceProcess::ServiceController

[ServiceProcessDescriptionAttribute("ServiceControllerDesc")]
public ref class ServiceController : Component

NameDescription
System_CAPS_pubmethodServiceController()

Initializes a new instance of the ServiceController class that is not associated with a specific service.

System_CAPS_pubmethodServiceController(String^)

Initializes a new instance of the ServiceController class that is associated with an existing service on the local computer.

System_CAPS_pubmethodServiceController(String^, String^)

Initializes a new instance of the ServiceController class that is associated with an existing service on the specified computer.

NameDescription
System_CAPS_pubpropertyCanPauseAndContinue

Gets a value indicating whether the service can be paused and resumed.

System_CAPS_protpropertyCanRaiseEvents

Gets a value indicating whether the component can raise an event.(Inherited from Component.)

System_CAPS_pubpropertyCanShutdown

Gets a value indicating whether the service should be notified when the system is shutting down.

System_CAPS_pubpropertyCanStop

Gets a value indicating whether the service can be stopped after it has started.

System_CAPS_pubpropertyContainer

Gets the IContainer that contains the Component.(Inherited from Component.)

System_CAPS_pubpropertyDependentServices

Gets the set of services that depends on the service associated with this ServiceController instance.

System_CAPS_protpropertyDesignMode

Gets a value that indicates whether the Component is currently in design mode.(Inherited from Component.)

System_CAPS_pubpropertyDisplayName

Gets or sets a friendly name for the service.

System_CAPS_protpropertyEvents

Gets the list of event handlers that are attached to this Component.(Inherited from Component.)

System_CAPS_pubpropertyMachineName

Gets or sets the name of the computer on which this service resides.

System_CAPS_pubpropertyServiceHandle

Gets the handle for the service.

System_CAPS_pubpropertyServiceName

Gets or sets the name that identifies the service that this instance references.

System_CAPS_pubpropertyServicesDependedOn

The set of services that this service depends on.

System_CAPS_pubpropertyServiceType

Gets the type of service that this object references.

System_CAPS_pubpropertySite

Gets or sets the ISite of the Component.(Inherited from Component.)

System_CAPS_pubpropertyStartType

Gets a value that indicates how the service represented by the ServiceController object starts.

System_CAPS_pubpropertyStatus

Gets the status of the service that is referenced by this instance.

NameDescription
System_CAPS_pubmethodClose()

Disconnects this ServiceController instance from the service and frees all the resources that the instance allocated.

System_CAPS_pubmethodContinue()

Continues a service after it has been paused.

System_CAPS_pubmethodCreateObjRef(Type^)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodDispose()

Releases all resources used by the Component.(Inherited from Component.)

System_CAPS_protmethodDispose(Boolean)

Releases the unmanaged resources used by the ServiceController and optionally releases the managed resources. (Overrides Component::Dispose(Boolean).)

System_CAPS_pubmethodEquals(Object^)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodExecuteCommand(Int32)

Executes a custom command on the service.

System_CAPS_protmethodFinalize()

Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection.(Inherited from Component.)

System_CAPS_pubmethodSystem_CAPS_staticGetDevices()

Retrieves the device driver services on the local computer.

System_CAPS_pubmethodSystem_CAPS_staticGetDevices(String^)

Retrieves the device driver services on the specified computer.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodGetService(Type^)

Returns an object that represents a service provided by the Component or by its Container.(Inherited from Component.)

System_CAPS_pubmethodSystem_CAPS_staticGetServices()

Retrieves all the services on the local computer, except for the device driver services.

System_CAPS_pubmethodSystem_CAPS_staticGetServices(String^)

Retrieves all the services on the specified computer, except for the device driver services.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodPause()

Suspends a service's operation.

System_CAPS_pubmethodRefresh()

Refreshes property values by resetting the properties to their current values.

System_CAPS_pubmethodStart()

Starts the service, passing no arguments.

System_CAPS_pubmethodStart(array<String^>^)

Starts a service, passing the specified arguments.

System_CAPS_pubmethodStop()

Stops this service and any services that are dependent on this service.

System_CAPS_pubmethodToString()

Returns a String containing the name of the Component, if any. This method should not be overridden.(Inherited from Component.)

System_CAPS_pubmethodWaitForStatus(ServiceControllerStatus)

Infinitely waits for the service to reach the specified status.

System_CAPS_pubmethodWaitForStatus(ServiceControllerStatus, TimeSpan)

Waits for the service to reach the specified status or for the specified time-out to expire.

NameDescription
System_CAPS_pubeventDisposed

Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)

You can use the ServiceController class to connect to and control the behavior of existing services. When you create an instance of the ServiceController class, you set its properties so 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 the ServiceController component in an administrative capacity. For example, you could create a Windows or Web application that sends custom commands to a service through the ServiceController instance. This would be useful, because the Service Control Manager (SCM) Microsoft Management Console snap-in does not support custom commands.

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.

System_CAPS_noteNote

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

Generally, the service author writes code that customizes the action associated with a specific command. For example, a service can contain code to respond to an ServiceBase::OnPause command. In that case, the custom processing for the Pause task runs before the system pauses the service.

The set of commands a service can process depends on its properties; for example, you can set the CanStop property for a service to false. This setting renders the Stop command unavailable on that particular service; it prevents you from stopping the service from the SCM by disabling the necessary button. If you try to stop the service from your code, the system raises an error and displays the error message "Failed to stop servicename."

The following example demonstrates the use of the ServiceController class to control the SimpleService service example.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft