ServiceController.Status Property
.NET Framework 4.5
Gets the status of the service that is referenced by this instance.
Assembly: System.ServiceProcess (in System.ServiceProcess.dll)
[ServiceProcessDescriptionAttribute("SPStatus")] public ServiceControllerStatus Status { get; }
Property Value
Type: System.ServiceProcess.ServiceControllerStatusOne of the ServiceControllerStatus values that indicates whether the service is running, stopped, or paused, or whether a start, stop, pause, or continue command is pending.
| Exception | Condition |
|---|---|
| Win32Exception | An error occurred when accessing a system API. |
| InvalidOperationException | The service was not found. |
The following example uses the ServiceController class to check the current status of the Telnet service. If the service is stopped, the example starts the service. If the service is running, the example stops the service.
// Toggle the Telnet service - // If it is started (running, paused, etc), stop the service. // If it is stopped, start the service. ServiceController sc = new ServiceController("Telnet"); Console.WriteLine("The Telnet service status is currently set to {0}", sc.Status.ToString()); if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending))) { // Start the service if the current status is stopped. Console.WriteLine("Starting the Telnet service..."); sc.Start(); } else { // Stop the service if its status is not set to "Stopped". Console.WriteLine("Stopping the Telnet service..."); sc.Stop(); } // Refresh and display the current service status. sc.Refresh(); Console.WriteLine("The Telnet service status is now set to {0}.", sc.Status.ToString());
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.