Service Management in C#.NET

Introduction

All of us know there are so many services running in the background without our knowledge. We can view, what are the services running on our system by typing services.msc in the Run of Start Button.

I will explain how to create a Web Application to manage the services running on our system. This application allows us to see all services present in our system. We can also start, stop, or pause the services. This application will show some details of all services, like their path and state..... I created this application in VS.NET 2003.

First, create a web application in C#. Name it as ServiceMgmt.

Design the UI as shown below.

UI Design

There are four linkbuttons, one dropdownlist, and seven labels in it.

Add a reference to System.Management(in Solution Explorer) as shown below.

System.Management(in Solution Explorer)

Next.

System.Management(in Solution Explorer) 

Add this code in Page_Load.

ManagementClass class1 = new ManagementClass("Win32_Service");

foreach (ManagementObject ob in class1.GetInstances())
{
    ListItem item1 = new ListItem();
    item1.Text = ob.GetPropertyValue("Name").ToString();
    item1.Value = ob.GetPropertyValue("Caption").ToString();
    DropDownList1.Items.Add(item1);
}

This will load all the services into the ComboBox.

Next, add this method.

private void showstatus()
{
    int i = 0;
    ManagementClass class1 = new ManagementClass("Win32_Service");

    foreach (ManagementObject ob in class1.GetInstances())
    {
        if (ob.GetPropertyValue("State").ToString() == "Running")
        {
            i++;
        }

        if (ob.GetPropertyValue("Name").ToString() == DropDownList1.SelectedItem.Text.ToString())
        {
            Label2.Text = "Description : " + ob.GetPropertyValue("Description");
            Label3.Text = "PathName : " + ob.GetPropertyValue("PathName");
            Label4.Text = "ServiceType : " + ob.GetPropertyValue("ServiceType");
            Label5.Text = "StartMode : " + ob.GetPropertyValue("StartMode");
            Label6.Text = "State : " + ob.GetPropertyValue("State");
        }
    }

    Label7.Text = string.Empty;
    Label7.Text = "Total Number of Services Running : " + i;
}

This will be called whenever you select a service from Combobox. This will show some of the details of the service.

Next, add this method.

private void servicemgmt(string status)
{
    ManagementPath path = new ManagementPath();
    path.Server = System.Environment.MachineName;
    path.NamespacePath = @"root\CIMV2";
    path.RelativePath = "Win32_service.Name='" + DropDownList1.SelectedItem.Text.ToString() + "'";

    ManagementObject service = new ManagementObject(path);
    ManagementBaseObject temp = service.InvokeMethod(status, null, null);
}

This method will start, stop, pause, and continue the service you selected from Combobox.

Next, copy this code to Start LinkButton_Click.

servicemgmt("StartService");
showstatus();

Next, copy this code to Stop LinkButton_Click.

servicemgmt("StopService");
showstatus();

Next, copy this code to Pause LinkButton_Click.

servicemgmt("PauseService");
showstatus();

Next, copy this code to Continue LinkButton_Click.

servicemgmt("ResumeService");
showstatus();

By this, we can control our services from IE.

I hope this code will be useful for all. The final screen will be like this.

Service management


Similar Articles