Home >Backend Development >C++ >How to Retrieve a List of Connected USB Devices in Windows?

How to Retrieve a List of Connected USB Devices in Windows?

Susan Sarandon
Susan SarandonOriginal
2025-01-25 00:47:101016browse

How to Retrieve a List of Connected USB Devices in Windows?

Get the list of USB devices that have been connected to the USB device

When processing USB devices, you often need to obtain a list of all connected USB devices. In the Windows environment, the

class of the name space can be used to complete this task. System.Management ManagementObjectSearcher First, add a reference to your project. After completing this operation, you can use the following code fragment to retrieve the connected USB device list:

System.Management The

method in the above code fragment returns a list of
<code class="language-csharp">using System;
using System.Collections.Generic;
using System.Management; // 需要在项目引用中添加 System.Management。

class Program
{
    static void Main(string[] args)
    {
        var usbDevices = GetUSBDevices();

        foreach (var usbDevice in usbDevices)
        {
            Console.WriteLine(
                $"设备ID:{usbDevice.DeviceID},PnP设备ID:{usbDevice.PnpDeviceID},描述:{usbDevice.Description}");
        }

        Console.ReadKey();
    }

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

        using var searcher = new ManagementObjectSearcher(
            @"Select * From Win32_USBHub");
        using var collection = searcher.Get();

        foreach (var 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>
objects, which contains

, GetUSBDevices() and USBDeviceInfo attributes. These attributes represent the unique identifier of the device, the description of the device ID and the device. DeviceID PNPDeviceID By using this code, you can easily obtain a list of all connected USB devices on the Windows computer, and access its attributes for further processing. Description

The above is the detailed content of How to Retrieve a List of Connected USB Devices in Windows?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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