Check Window service is running and Check is Stop using .NET (Csharp & VB)

5. March 2010

Author: Aamir Hasan

ServiceController class to control the SimpleService service.
Add reference to  System.ServiceProcess.

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

 

C#

static void Main(string[] args)
{
try
{
ServiceController controller = new ServiceController("SERVICENAME");

if (controller.Status.Equals(ServiceControllerStatus.Running)
&& controller.CanStop)
{
controller.Stop();
Console.WriteLine("Service Stopped");
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

VB.NET

Shared Sub Main(ByVal args() As String)
Try
Dim
controller As New ServiceController("SERVICENAME")

If controller.Status.Equals(ServiceControllerStatus.Running)_
          AndAlso controller.CanStop Then
controller.Stop()
Console.WriteLine("Service Stopped")
End If
Console.ReadLine()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub



Reference http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx

Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, .NET



User Name: Guest

Your Ip: 38.107.191.94
Time: