This is my second translation.ArticleCompared with the previous article (Introduction to ASP. NET 2.0 (ASP. NET 2.0), I feel a lot better about myself, but there are still some shortcomings. I still need to make corrections in future translations.
C #. netService Manager
Author:Sateesh Kumar2006-5-31
Translator:Sparon2006-5-31
This chapter will show you how to createWebApplicationProgramTo manage services on machines.
Original article address:Http://www.c-sharpcorner.com/UploadFile/satisharveti/tocontrolservicesrunninginasystem05302006100035AM/tocontrolservicesrunninginasystem. Aspx
We know that many programs running in the background are not known to us.Services. MSCTo view these services.
I will explain how to createWebApplications to manage services on machines. This program allows us to view all the services on our computers and can run, stop, or pause the services. It can also view the detailed information of all services, such as the path and status. This program can beVs. NET 2003.
First, createC # WebApplication. Name itServicemgmt
The page is as follows:
On this page4Hyperlink,1ItemsDropdownlistAnd7ItemsLabels.
Add in solution ManagerSystem. ManagementAssociation.
Next step:
Replace theseCodeAddPage_loadEvent:
Managementclass class1 =NewManagementclass ("win32_service ");
Foreach(Managementobject obInClass1.getinstances ())
{
Listitem Item1 =NewListitem ();
Item1.text = OB. getpropertyvalue ("name"). tostring ();
Item1.value = OB. getpropertyvalue ("caption"). tostring ();
Dropdownlist1.items. Add (Item1 );
}
This code will load all services on the machineComboBox.
Add more methods:
Private VoidShowstatus ()
{
IntI = 0;
Managementclass class1 =NewManagementclass ("win32_service ");
Foreach(Managementobject obInClass1.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 code shows the details of the selected service.
Add more methods:
Private VoidServicemgmt (StringStatus)
{
Managementpath Path =NewManagementpath ();
Path. Server = system. environment. machinename;
Path. namespacepath = @ "Root \ cimv2 ";
Path. relativepath = "win32_service.name = '" + dropdownlist1.selecteditem. Text. tostring () + "'";
Managementobject service =NewManagementobject (PATH );
Managementbaseobject temp = service. invokemethod (status,Null,Null);
}
This code is used to run, stop, pause, and continue the service.
Copy this code to startLinkbutton_clickEvent:
Servicemgmt ("startservice ");
Showstatus ();
Copy this code to stopLinkbutton_clickEvent:
Servicemgmt ("stopservice ");
Showstatus ();
Copy this code to pauseLinkbutton_clickEvent:
Servicemgmt ("pauseservice ");
Showstatus ();
Copy this code to continueLinkbutton_clickEvent:
Servicemgmt ("resumeservice ");
Showstatus ();
So far, we can useIEControl the services on our machines.
I hope this code will help you..The final result is as follows::
Download Full-text code: servicemgmt.rar
Appendix: wmiopennotepad.rar