SIGN UP MEMBER LOGIN:    
ARTICLE

Service Management in C#.NET

Posted by Sateesh Arveti Articles | C# Tutorials May 31, 2006
This article discusses how to create a Web Application to manage the services running on our system.
Reader Level:
Download Files:
 

All of us know there are so many services running in background without our knowledge. We can view, what are the services running on our system by typing services.msc in 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 its path, state.... created this application in VS.NET 2003.

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

Design the UI as shown below:

There are 4 linkbuttons, one dropdownlist and 7 labels in it.

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

Next,

 

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, 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. Final screen will be like this:

Login to add your contents and source code to this article
share this article :
post comment
 

Could this article be updated to reflect the permission issues encountered with ClickOnce deployment and Windows 7?

Posted by Mike Hillman Apr 13, 2010

Dear Friend

I think you can overcome this problem taking the following steps(any one or both)

1.Give the administrative right to your ASPNET user

or

2.In web.config mentin the following xml tag

<identity impersonate=true/>

If any problem mail me at

bmanab@gmail.com at subject line quaries from c# corner

Posted by Manab Basu Jul 11, 2007

 

I am trying to run this application but i am getting following error

System.Management.ManagementException: Access denied

impersonate="true" is there in web.config

on following line

foreach(ManagementObject ob in class1.GetInstances())


Please help me

Amol

Posted by Amol Mirashi Jun 07, 2006
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor