C # obtain information about Windows Services

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.