In this tutorial we'll be retrieving a list of the available Windows services and get some details about each service, all using the ManagementObject of ManagementClass.
The Windows Services are available in the Microsoft Management Console. You can go to Administrative Tools -> Services and get a list of all the services. But how do you go about listing them in your own .NET application? Well, here’s how you do it using C#. First thing, you need to add a new reference:
The .NET reference you need to add is System.Management:
Of course, with the addition of the reference, it is appropriate to also add the using statement, and another using statement to System.Collections since we’ll use a dynamically allocated array later:
using System.Management;
using System.Collections;
The form is a simple design, one ListBox to hold the services, one TextBox (Multiline) to hold the description of the service and four labels to show all the other information about the service. In the end it should look similar to this:
Now we’re ready to plug in the code. First create a new array in the class, one that will bind the index of the ListBox item with the actual name of the service. This will help us later when we want to retrieve details for the selected service.
ArrayList arrServices = new ArrayList();
Since we don’t know how many services are available, we don’t create a typical string array but an ArrayList, this way we don’t have to define a size.
The very first lines of code go into the form’s load event:
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);
}
}
They retrieve the services by creating a new instance of the ManagementClass that binds to the WMI Win32_Service class. Then there’s a loop that goes through every ManagementObject instance provided by this class. Inside it a new array is created, this time a fixed-size one. This will store the ID of the ListBox item next to the caption of each service. The Name value is a short unique name by which we can later refer to the service, while the Caption is the full name of the service. This array is stored in the ArrayList object, which basically creates a two-dimensional array.
Now that we have the Windows Services being retrieved, the remaining part is getting more details about them, which is really done through GetPropertyValue() just as we did to retrieve the Caption and Name values earlier:
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");
}
}
}
We're done. Here's how it looks like in action:
If you don’t care much for real-time information on the services, you can store all the information in the array the first time that the services are being loaded and then just retrieve it using the ID of the selected ListBox item as the index of the array.
Soon another tutorial will follow on the same subject, but which will retrieve services the .NET 2.0 way, which uses the ServiceController class. There you’ll also see how to start, stop and restart a service.
Here’s the full source code for this C# project:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Collections;
namespace Services
{
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");
}
}
}
}
}