Get List of connected USB Devices

前端 未结 8 1634
再見小時候
再見小時候 2020-11-22 08:27

How can I get a list of all the connected USB devices on a windows computer?

相关标签:
8条回答
  • 2020-11-22 09:17

    Add a reference to System.Management for your project, then try something like this:

    namespace ConsoleApplication1
    {
      using System;
      using System.Collections.Generic;
      using System.Management; // need to add System.Management to your project references.
    
      class Program
      {
        static void Main(string[] args)
        {
          var usbDevices = GetUSBDevices();
    
          foreach (var usbDevice in usbDevices)
          {
            Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
                usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
          }
    
          Console.Read();
        }
    
        static List<USBDeviceInfo> GetUSBDevices()
        {
          List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
    
          ManagementObjectCollection collection;
          using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
            collection = searcher.Get();      
    
          foreach (var device in collection)
          {
            devices.Add(new USBDeviceInfo(
            (string)device.GetPropertyValue("DeviceID"),
            (string)device.GetPropertyValue("PNPDeviceID"),
            (string)device.GetPropertyValue("Description")
            ));
          }
    
          collection.Dispose();
          return devices;
        }
      }
    
      class USBDeviceInfo
      {
        public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
        {
          this.DeviceID = deviceID;
          this.PnpDeviceID = pnpDeviceID;
          this.Description = description;
        }
        public string DeviceID { get; private set; }
        public string PnpDeviceID { get; private set; }
        public string Description { get; private set; }
      }
    }
    
    0 讨论(0)
  • 2020-11-22 09:27

    If you change the ManagementObjectSearcher to the following:

    ManagementObjectSearcher searcher = 
           new ManagementObjectSearcher("root\\CIMV2", 
           @"SELECT * FROM Win32_PnPEntity where DeviceID Like ""USB%"""); 
    

    So the "GetUSBDevices() looks like this"

    static List<USBDeviceInfo> GetUSBDevices()
    {
      List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
    
      ManagementObjectCollection collection;
      using (var searcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_PnPEntity where DeviceID Like ""USB%"""))
        collection = searcher.Get();      
    
      foreach (var device in collection)
      {
        devices.Add(new USBDeviceInfo(
        (string)device.GetPropertyValue("DeviceID"),
        (string)device.GetPropertyValue("PNPDeviceID"),
        (string)device.GetPropertyValue("Description")
        ));
      }
    
      collection.Dispose();
      return devices;
    }
    

    }

    Your results will be limited to USB devices (as opposed to all types on your system)

    0 讨论(0)
提交回复
热议问题