Use WMI to get information including the operating system’s name in C#


WMI (Windows Management Instrumentation) lets you use SQL-like statements to ask the computer about itself. This example uses it to get:

  • The operating system name including its edition (Home, Ultimate, etc.), version, and Service Pack number
  • The number of logical processors
  • The number of bits the system uses (32 or 64)

When the program loads, it uses the following code to display these values in labels.

// Add a reference to System.Management.
using System.Management;
// Display the operating system's name.
private void Form1_Load(object sender, EventArgs e)
    // Get the OS information.
    // For more information from this query, see:
    string os_query = "SELECT * FROM Win32_OperatingSystem";
    ManagementObjectSearcher os_searcher =
        new ManagementObjectSearcher(os_query);
    foreach (ManagementObject info in os_searcher.Get())
      lblCaption.Text =
      lblVersion.Text = "Version " +
        info.Properties["Version"].Value.ToString() +
        " SP " +
        + "." +

    // Get number of processors.
    // For more information from this query, see:
    string cpus_query = "SELECT * FROM Win32_ComputerSystem";
    ManagementObjectSearcher cpus_searcher =
        new ManagementObjectSearcher(cpus_query);
    foreach (ManagementObject info in cpus_searcher.Get())
      lblCpus.Text =
        + " processors";

    // Get 32- versus 64-bit.
    // For more information from this query, see:
    string proc_query = "SELECT * FROM Win32_Processor";
    ManagementObjectSearcher proc_searcher =
        new ManagementObjectSearcher(proc_query);
    foreach (ManagementObject info in proc_searcher.Get())
      lblBits.Text = 
        info.Properties["AddressWidth"].Value.ToString() + "-bit";

The code creates a ManagementObjectSearcher to execute the WMI query SELECT * FROM Win32_OperatingSystem to get information about the operating system. It uses the searcher’s Get method to get a collection holding the desired information. In this example, the Get call returns a collection holding a single object. The code uses a foreach loop to loop over that object.

Inside the loop, the code gets the returned object’s Caption, Version, ServicePackMajorVersion, and ServicePackMinorVersion properties.

Next the program uses similar code to execute the WMI query SELECT * FROM Win32_ComputerSystem to get the number of logical processors.

It finishes by executing the query SELECT * FROM Win32_Processor to determine whether this is a 32-bit or 64-bit operating system.

Download Example   Follow me on Twitter   RSS feed

This entry was posted in system, WMI and tagged , , , , , , , , , , , , , . Bookmark the permalink.