Starting and Stopping Windows Services with C#
2008 06 16 – 8:38 pmThe other day I found myself needing to write a quick application to start and stop services on my desktop since I didn’t need them all running all the time. Specifically, I have a copy of the SQL Server 2008 CTP that I’m fooling around with and it is my observation that running the SQL Server services (even express edition) has an adverse affect on system performance on Windows Vista (pick your edition) system. So, rather than always opening the Administrative Tools and going through the Microsoft provided tools to start and stop services, I decided to embrace the learning opportunity and write a little app to handle it my self.
For the purpose of this demonstration, we will get a list of all the services that are available on your machine and throw it into a DataTable.
First thing’s first. You’ll need to add the
System.Management
and
System.ServiceProcess
libraries to your project. This will allow you access to the classes we’ll need to get the list of services. The following code will create an instance of the ManagementClass, create a DataTable and load the list of services we acquire through the ManagementClass class into the DataTable.
dt = new DataTable();
DataColumn dc = new DataColumn();
dc.DataType = Type.GetType("System.String");
dc.ColumnName = "Name";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = Type.GetType("System.String");
dc.ColumnName = "Caption";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = Type.GetType("System.String");
dc.ColumnName = "Description";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = Type.GetType("System.String");
dc.ColumnName = "PathName";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = Type.GetType("System.String");
dc.ColumnName = "StartMode";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = Type.GetType("System.String");
dc.ColumnName = "ServiceType";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = Type.GetType("System.String");
dc.ColumnName = "State";
dt.Columns.Add(dc);
ManagementClass mgt = new ManagementClass("Win32_Service");
foreach (ManagementObject ob in mgt.GetInstances())
{
DataRow dr = dt.NewRow();
dr["Name"] = ob.GetPropertyValue(”Name”).ToString();
dr["Caption"] = ob.GetPropertyValue(”Caption”).ToString();
dr["Description"] = ob.GetPropertyValue(”Description”).ToString();
dr["PathName"] = ob.GetPropertyValue(”PathName”).ToString();
dr["StartMode"] = ob.GetPropertyValue(”StartMode”).ToString();
dr["ServiceType"] = ob.GetPropertyValue(”ServiceType”).ToString();
dr["State"] = ob.GetPropertyValue(”State”).ToString();
dt.Rows.Add(dr);
}
Now let me offer a quick explanation the columns we added to the DataTable.
- Name – the actual name of the service. When viewing the service list in the Administrative Tools of your windows system, you are not actually viewing the service name. You are viewing the caption.
- Caption – The caption is actually referred to as the service’s name in the Administrative Tools.
- Description – A brief description of the service and what it does.
- PathName – The path of the service .exe or .dll.
- StartMode – Manual, Automatic, etc.
- ServiceType – Can’t remember off the top of my head and I’m too lazy to find out.
- State – Current state of the service (Running, Stopped, Paused).
Now, say you want to start or stop a particular service. Use the following:
ServiceController ctlr = new System.ServiceProcess.ServiceController(); ctlr.ServiceName = selectedItem; // The Name value for the servicectlr.Stop(); ctlr.Start(); ctlr.Pause(); ctlr.Continue(); ctlr.Resume();
And that’s about it. Pretty simple huh?
A professional software developer on the .Net and LAMP platforms.
I enjoy walks on the beach, SQL Server, video games, and college sports.