Windows Services Admin: Control Your Windows Services


We will create an application to control Windows Services on our local Computer and also on remote computers. You can simultaneously Stop or Start multiple Services on the local computer or on the specified remote computer.

Note that you will need to have permissions to control windows services.

We make use of the ServiceController class found in the System.ServiceProcess namespace of the .Net Framework. This class represents a Windows Service and allows you to connect to a Windows Service, to manipulate the service and get information about the service.

We encourage you to download the samples source code to get a complete picture of the program.



Figure 1: Program Demonstration



Figure 2: Stop the selected windows services by click on the Stop button

Lets start by creating a Visual C# Project of Type Windows Application

On the WebForm1 that gets created in the project, add the following controls from the ToolBox:

Control Type Property New Value
Label Text Server Name
Text Box Name TxtServer
Text Local
Button Name BtnOK
Text Get Services
Button Text Stop
Button Text Start
DataGrid Caption Text Services List



Figure 3: Form Design

Double click the btnOK button (with caption Get Services) and add the following code to the event handler.

DisplayServices();

Next, we will define the function DisplayServices in our program. As you will soon see, this function is re-used in the program.

This function does the work of getting the Windows Services information. The GetServices method of the ServiceController returns an array of ServiceController objects that provide us access to the properties of Services and allow us to manipulate the Windows services programmatically. We extract the Name and Status of each Windows Service into an ArrayList and bind this ArrayList to the DataGrid to display the information on Services.

The function catches exception condition and notifies the user if any exception occurs.

private void DisplayServices()
{
string strServerName;
ServiceController[] oArrServices;
try
{
if ((txtServer.Text.Length == 0 ) || (txtServer.Text == "Local"))
{
oArrServices = ServiceController.GetServices();
}
else
oArrServices = ServiceController.GetServices(txtServer.Text);
ALServiceInfo.Clear();
foreach (ServiceController oSC in oArrServices)
{
ALServiceInfo.Add(
new ServiceInfo(oSC.ServiceName, oSC.Status.ToString()));
}
dataGrid1.DataSource = ALServiceInfo;
dataGrid1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show("Error Occured: " + ex.Message);
}
}

Start and Stop Button Handlers

Shown below is the code that allows the user to Start or Stop the selected Window Service(s) running on the local machine or on a remote machine (provided user has appropriate permissions).

The function ProcessRequest is used to Start and Stop the Windows Service. The input parameter bStop should be set as True when the function is invoked to Stop the service and set to False when the function is invoked to Start the service.

Take a look at the function ProcessRequest (Download the Code File provided at the top of the article to view the complete listing).

We navigate through the rows in the DataGrid and examine the Selected rows. We get access to the Windows Service functions through the ServiceController created for the selected Service based on the local machine or a specified remote computer.

If the input parameter bStop is set to True, we check if we are allowed to Stop the service, and if so, Stop the service. The function to display the list of Windows Services is called again to Refresh the form view. In most cases the Service that is stopped will display a status of Pending Stop. You can click the Get Services button to refresh the list and the status will eventually get updated to Stopped as the service is completely stopped.

Similarly, if the input parameter bStop is set to False, we check if the service is not already in the Stated or PendingStart status, and if so, invoke the function to Start the service.

private void ProcessRequest(Boolean bStop)
{
ServiceController oSC;
for (int i = 0;i< ALServiceInfo.Count;i ++)
{
if (dataGrid1.IsSelected(i))
{
if ((txtServer.Text.Length == 0) || (txtServer.Text == "Local"))
{
oSC =
new ServiceController(dataGrid1[i,0].ToString());
}
else
{
oSC =
new ServiceController(dataGrid1[i,0].ToString(),txtServer.Text);
}
if (bStop)
{
if (oSC.CanStop)
{
oSC.Stop();
}
}
else
{
if ((oSC.Status.ToString() != "Started") & (oSC.Status.ToString() != "Pending
Start"))
{
oSC.Start();
}
}
}
}
DisplayServices();
}

Note: To make Multiple Selections in the displayed Services List, press the Control key and make the selections.

Further Enhancements: This program can be optimized further. One of the enhancements could be to include a Timer control and refresh the Services List at certain time intervals.

Up Next
    Ebook Download
    View all
    Learn
    View all