How do you exit  a Windows Service programmatically from within the service?

Posted on 2005-04-14
Last Modified: 2010-04-16
I am creating a windows service application. It has a configuration file that is checked at startup.  If the configuration file isn't correct, I want to stop the service.  What is the best way to do this?  A C# code example would be very useful.
Question by:rfischernw7
    LVL 7

    Accepted Solution

    You should be able to use the ServiceController object to do this, from inside the Service class:

          ServiceController sc = new ServiceController(this.ServiceName);

    I haven't actually tested this, but it should work. Let me know how it goes, and I'll look into it deeper if you have problems with it.

    Author Comment

    I will give it a try

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now