yanhuatangtang
will-well
2018-05-04 09:55
采纳率: 0%
浏览 2.1k

C#读取设备管理器中某个设备信息:属性--详细信息-容器ID

如何通过访问设备管理器中的属性--详细信息--容器ID,采用什么方式可以获得?图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • caozhy
    点赞 评论
  • q446002320
    BMKB 2018-05-05 08:04

    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; }
    }
    }

    点赞 评论
  • chxg99
    chxg99 2018-05-05 11:02
    点赞 评论
  • yanhuatangtang
    will-well 2018-05-28 07:49

    貌似这个无法通过读取设备描述符PID/VID等信息一样读取ContainerID,因为在C#中没有定义类似的类。

    点赞 评论

相关推荐