Querying Device Manager Information via WMI in C#

To interact with Windows Management Instrumentation (WMI) and retrieve system hardware details, the System.Management namespace must be referenced in your project. WMI provides a standardized interface for accessing management data about operating systems, devices, and applications.

WMI Class Categories

WMI classes are organized into hardware and operating system categories. While specific classes allow for targeted queries, the Win32_PnPEntity class serves as a comprehensive wrapper for all Plug and Play entities visible in the Device Manager.

Hardware Classes

Specific classes exist for granular hardware control, including Win32_Processor (CPU), Win32_PhysicalMemory (RAM), Win32_Keyboard, Win32_PointingDevice, and storage devices like Win32_DiskDrive and Win32_CDROMDrive. System components are covered by Win32_BaseBoard, Win32_BIOS, and Win32_SystemSlot. Connectivity is handled by Win32_NetworkAdapter, Win32_USBController, Win32_SerialPort, and Win32_ParallelPort. Graphics and audio are managed through Win32_VideoController, Win32_DesktopMonitor, and Win32_SoundDevice.

Operating System Classes

OS-level information can be retrieved using classes such as Win32_OperatingSystem, Win32_ComputerSystem, and Win32_TimeZone. Disk management involves Win32_LogicalDisk, Win32_DiskPartition, and Win32_PageFile. System services, processes, and user accounts are accessible via Win32_Service, Win32_Process, and Win32_UserAccount.

Enumerating All Plug and Play Devices

The following example demonstrates how to retrieve the name of every device listed in the Device Manager using the Win32_PnPEntity class. This method utilizes a ManagementObjectSearcher to execute a WQL query.

using System;
using System.Collections.Generic;
using System.Management;

public static List<string> FetchAllDeviceNames()
{
    var deviceNameList = new List<string>();
    
    // WQL query to select all PnP entities
    var query = "SELECT Name FROM Win32_PnPEntity";
    
    using (var searcher = new ManagementObjectSearcher(query))
    using (var collection = searcher.Get())
    {
        foreach (ManagementObject entity in collection)
        {
            // Safely retrieve the Name property
            var deviceName = entity["Name"]?.ToString();
            if (!string.IsNullOrEmpty(deviceName))
            {
                deviceNameList.Add(deviceName);
            }
        }
    }
    
    return deviceNameList;
}

Querying Specific Device Properties

When searching for specific devices, you can filter the query using the WHERE clause. Note that device properties may return either single strings or string arrays (collections). The following code retrieves the Hardware ID property for devices matching a specific name pattern.

using System;
using System.Collections.Generic;
using System.Management;

public static List<string> GetDeviceHardwareIds(string filterName)
{
    var hardwareIds = new List<string>();
    
    // Parameterized query logic (ensure filterName is sanitized in production)
    var wqlString = $"SELECT HardwareID FROM Win32_PnPEntity WHERE Name LIKE '%{filterName}%'";
    
    using (var searcher = new ManagementObjectSearcher(wqlString))
    using (var results = searcher.Get())
    {
        foreach (ManagementObject device in results)
        {
            // HardwareID is typically a string array
            var idArray = device["HardwareID"] as string[];
            
            if (idArray != null)
            {
                foreach (var id in idArray)
                {
                    hardwareIds.Add(id);
                }
            }
        }
    }
    
    return hardwareIds;
}

You can inspect the Properties collection of any ManagementObject to discover all available attributes for a specific device.

Tags: C# WMI System.Management hardware information Win32_PnPEntity

Posted on Tue, 13 Oct 2026 16:52:35 +0000 by cutups