.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> System Process >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to determine if window service is running and also to stop it

Posted By:Rajshree       Posted Date: October 28, 2010    Points: 15    Category: System Process    URL: http://www.dotnetspark.com  

The ServiceController class makes it easy to retrieve information about a windows service and manipulate it. Here's some code. Before running this example, make sure you have added a reference to System.ServiceProcess
 

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);
    }
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend