To add a reference to system. Management. Below isProgramInterface:
Below isCodeIt is easy to understand:
Using System; Using System. Collections. Generic; Using System. componentmodel; Using System. Data; Using System. drawing; Using System. LINQ; Using System. text; Using System. Windows. forms; Using System. Management; Using System. collections; Namespace Winformservice { Public Partial Class Form1: FORM { Public Form1 () {initializecomponent ();} arraylist arrservices = New Arraylist (); Private Void Form1_load ( Object Sender, eventargs e ){// Create a new managementclass object binded to the win32_service WMI class Managementclass mcservices = New Managementclass (" Win32_service "); // Loop through each service Foreach (Managementobject moservice In Mcservices. getinstances ()){ // Create a new array that holds the ListBox item ID and service name String [] Srvarray = New String [2]; srvarray [0] = lstservices. Items. Add (moservice. getpropertyvalue (" Caption "). Tostring (). tostring (); srvarray [1] = moservice. getpropertyvalue (" Name "). Tostring (); // Store the array in the arraylist Arrservices. Add (srvarray );}} Private Void Lstservices_selectedindexchanged ( Object Sender, eventargs e) {managementclass mcservices = New Managementclass (" Win32_service ");// Loop through each service Foreach (Managementobject moservice In Mcservices. getinstances ()){ // Get back the array with the index of the selected ListBox item from the arraylist String [] Srvarray = ( String []) Arrservices [lstservices. selectedindex]; // If the current service name If (Moservice. getpropertyvalue (" Name "). Tostring () = srvarray [1]) {// Set the fields accordingly Txtdesc. Text = moservice. getpropertyvalue (" Description "). Tostring (); lblpath. Text =" Path: "+ Moservice. getpropertyvalue (" Pathname "); Lbltype. Text =" Type: "+ Moservice. getpropertyvalue (" Servicetype "); Lblstate. Text =" State: "+ Moservice. getpropertyvalue (" State "); Lblstartup. Text =" Start-up type: "+ Moservice. getpropertyvalue (" Startmode ");}}}}}
Process two events, one is formload, and the other is the list selectindexchanged event.