Introduction
Have you ever searched for something small on the Internet, only to find it blown up and confusing? If so, you are like me. Below, I have listed how to use the ServiceController
to control installed services. After understanding the code below, you will be able to start, stop, and get the status of a service.
Using the code
The first thing you need to do is, make a reference to System.ServiceProcess
in the Solution Explorer of Visual Studio. Some of the veterans out there are probably asking why I am putting these pictures in here. The reason: it is for newbies like me who are not that familiar with adding namespaces and components.


You will see that I added what I call [QUICK CODE]. This is for those who know what they are doing, or if you are like me and you are impatient. I stripped out most comments, and it doesn't display any thing.
The larger section of code is what I use in the downloadable content. It is a console application that checks the status of the ImapiService
and performs the opposite operation that it is already doing. Meaning, if the service is stopped, then I start it. If it is running, I stop it.
Once again, this is a very basic description of the ServiceController
class. I hope this helps.
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
namespace Using_ServiceController{
class Program{
static void Main(string[] args){
ServiceController myService = new ServiceController();
myService.ServiceName = "ImapiService";
string svcStatus = myService.Status.ToString();
if (svcStatus == "Running"){
myService.Stop();
}else if(svcStatus == "Stopped"){
myService.Start();
}else{
myService.Stop();
}
}
}
}
The complete code is here:
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
namespace Using_ServiceController
{
class Program
{
static void Main(string[] args)
{
ServiceController myService = new ServiceController();
myService.ServiceName = "ImapiService";
string svcStatus = myService.Status.ToString();
if (svcStatus == "Running")
{
Console.WriteLine(myService.ServiceName + " is in a " +
svcStatus + "State");
Console.WriteLine("Attempting to Stop!");
myService.Stop();
string svcStatusWas = "";
while (svcStatus != "Stopped")
{
svcStatusWas = svcStatus;
myService.Refresh();
svcStatus = myService.Status.ToString();
}
Console.WriteLine("Service Stopped!!");
}
else if (svcStatus == "Stopped")
{
Console.WriteLine(myService.ServiceName +
" is in a " + svcStatus + "State");
Console.WriteLine("Attempting to Start!");
myService.Start();
string svcStatusWas = "";
while (svcStatus != "Running")
{
if (svcStatus != svcStatusWas)
{
Console.WriteLine("Status: " + svcStatus);
}
svcStatusWas = svcStatus;
myService.Refresh();
svcStatus = myService.Status.ToString();
}
Console.WriteLine("Service Started!!");
}
else
{
myService.Stop();
Console.WriteLine("Status: " + svcStatus);
while (svcStatus != "Stopped")
{
myService.Refresh();
svcStatus = myService.Status.ToString();
}
Console.WriteLine("Service Stopped!!");
}
Console.WriteLine("----Sleeping----");
System.Threading.Thread.Sleep(30000);
}
}
}
Points of interest
For more information on the ServiceController
class, check out MSDN.