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

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.
Who is Participating?
CeiledConnect With a Mentor Commented:
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.
rfischernw7Author Commented:
I will give it a try
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.