Use WMI to get operating system information in C#

[WMI]

When the program starts, it executes the WMI query SELECT * FROM Win32_OperatingSystem. It loops through the results and calls subroutine GetValue for each of the many system parameters that should be available. The following code shows just a couple of the GetValue calls

// Get and display OS properties.
private void Form1_Load(object sender, EventArgs e)
{
    ManagementObjectSearcher os_searcher = 
        new ManagementObjectSearcher(
            "SELECT * FROM Win32_OperatingSystem");
    foreach (ManagementObject mobj in os_searcher.Get())
    {
        GetValue(mobj, "BootDevice");
        GetValue(mobj, "BuildNumber");
        GetValue(mobj, "BuildType");
        GetValue(mobj, "Caption");
        GetValue(mobj, "CodeSet");
        ...
    }

    // Auto-size the columns.
    foreach (ColumnHeader col in lvwResults.Columns)
    {
        col.Width = -2;
    }
}

The code first creates a ManagementObjectSearcher to execute the query. (To use this class, add a reference to System.Management and add a using statement for System.Management.)

Next the code loops through the returned ManagementObjects (there should be only one) and calls the GetValue method to get a bunch of values from the ManagementObject.

The following code shows the GetValue method.

// Get a value from the ManagementObject.
private void GetValue(ManagementObject mobj, string property_name)
{
    string value;
    try
    {
        value = mobj[property_name].ToString();
    }
    catch (Exception ex)
    {
        value = "*** Error: " + ex.Message;
    }

    lvwResults.Items.Add(property_name).SubItems.Add(value);
}

This method uses the property’s name as a ManagementObject index. It converts the property into a string (if possible) and adds the name and its value to the program’s ListView control.

This example fetches these properties:

  • BootDevice
  • BuildNumber
  • BuildType
  • Caption
  • CodeSet
  • CountryCode
  • CreationClassName
  • CSCreationClassName
  • CSDVersion
  • CSName
  • CurrentTimeZone
  • DataExecutionPrevention_Available
  • DataExecutionPrevention_32BitApplications
  • DataExecutionPrevention_Drivers
  • DataExecutionPrevention_SupportPolicy
  • Debug
  • Description
  • Distributed
  • EncryptionLevel
  • ForegroundApplicationBoost
  • FreePhysicalMemory
  • FreeSpaceInPagingFiles
  • FreeVirtualMemory
  • InstallDate
  • LargeSystemCache
  • LastBootUpTime
  • LocalDateTime
  • Locale
  • Manufacturer
  • MaxNumberOfProcesses
  • MaxProcessMemorySize
  • MUILanguages[]
  • Name
  • NumberOfLicensedUsers
  • NumberOfProcesses
  • NumberOfUsers
  • OperatingSystemSKU
  • Organization
  • OSArchitecture
  • OSLanguage
  • OSProductSuite
  • OSType
  • OtherTypeDescription
  • PAEEnabled
  • PlusProductID
  • PlusVersionNumber
  • Primary
  • ProductType
  • QuantumLength
  • QuantumType
  • RegisteredUser
  • SerialNumber
  • ServicePackMajorVersion
  • ServicePackMinorVersion
  • SizeStoredInPagingFiles
  • Status
  • SuiteMask
  • SystemDevice
  • SystemDirectory
  • SystemDrive
  • TotalSwapSpaceSize
  • TotalVirtualMemorySize
  • TotalVisibleMemorySize
  • Version
  • WindowsDirectory


Download Example   Follow me on Twitter   RSS feed   Donate




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

Leave a Reply

Your email address will not be published. Required fields are marked *