Home > Backend Development > C++ > How Do I Get a Comprehensive List of Connected USB Devices in Windows Using WMI?

How Do I Get a Comprehensive List of Connected USB Devices in Windows Using WMI?

DDD
Release: 2025-01-25 00:37:09
Original
589 people have browsed it

How Do I Get a Comprehensive List of Connected USB Devices in Windows Using WMI?

Accessing a Comprehensive List of Connected USB Devices on Windows Systems

Efficient hardware management often necessitates inspecting and interacting with connected peripherals. USB devices, being ubiquitous, frequently require programmatic access for inventory, diagnostics, or other administrative tasks. Windows offers several methods for retrieving this information; one powerful approach is using the Windows Management Instrumentation (WMI) framework.

WMI provides detailed system and hardware information, including a complete picture of connected USB devices. This requires incorporating the System.Management assembly into your project. The following C# code example demonstrates how to retrieve this data:

<code class="language-csharp">using System;
using System.Collections.Generic;
using System.Management; // Requires adding System.Management to project references

namespace USBDeviceEnumeration
{
    class Program
    {
        static void Main(string[] args)
        {
            List<USBDeviceInfo> usbDevices = GetUSBDevices();

            foreach (USBDeviceInfo device in usbDevices)
            {
                Console.WriteLine($"Device ID: {device.DeviceID}, PNP Device ID: {device.PnpDeviceID}, Description: {device.Description}");
            }

            Console.ReadKey();
        }

        static List<USBDeviceInfo> GetUSBDevices()
        {
            List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_USBHub"))
            using (ManagementObjectCollection collection = searcher.Get())
            {
                foreach (ManagementObject device in collection)
                {
                    devices.Add(new USBDeviceInfo(
                        (string)device.GetPropertyValue("DeviceID"),
                        (string)device.GetPropertyValue("PNPDeviceID"),
                        (string)device.GetPropertyValue("Description")
                    ));
                }
            }
            return devices;
        }
    }

    class USBDeviceInfo
    {
        public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
        {
            DeviceID = deviceID;
            PnpDeviceID = pnpDeviceID;
            Description = description;
        }
        public string DeviceID { get; private set; }
        public string PnpDeviceID { get; private set; }
        public string Description { get; private set; }
    }
}</code>
Copy after login

This code uses a ManagementObjectSearcher with the query "Select * From Win32_USBHub" to retrieve all USB hubs. Each ManagementObject represents a hub, providing details about it and connected devices. The code iterates through these objects, extracting the DeviceID, PNPDeviceID, and Description for each device. The resulting USBDeviceInfo objects offer comprehensive data for various applications, such as device management or system diagnostics. This WMI approach provides a robust and efficient method for obtaining a complete list of connected USB devices within a Windows environment.

The above is the detailed content of How Do I Get a Comprehensive List of Connected USB Devices in Windows Using WMI?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template