滴水静禅天
扫描关注滴水静禅天

扫码加微信:-)

.net下如何快速查看即插即用设备信息

滴水静禅天2017-01-15信息计算 1276
using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
namespace Basic
{

 public  class USBDeviceInfo
  {
     public USBDeviceInfo(string deviceID, string pnpDeviceID, string description, string manufacturer, string[] hardwareID,string caption)
    {
      this.DeviceID = deviceID;
      this.PnpDeviceID = pnpDeviceID;
      this.Description = description;
      this.Manufacturer = manufacturer;
      this.HardwareID =hardwareID;
      this.Caption = caption;
    }
    public string DeviceID { get; private set; }
    public string PnpDeviceID { get; private set; }
    public string Description { get; private set; }
    public string[] HardwareID { get; private set; }
    public string Manufacturer { get; private set; }
    public string Caption { get; private set; }
  }
 
 public  class GetUsbInfo
  {
   public static List<USBDeviceInfo> GetUSBDevices()
    {
      List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
      ManagementObjectCollection collection;
      using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPEntity"+" WHERE Manufacturer='Advantech'"))
        collection = searcher.Get();     
      foreach (var device in collection)
      {
        devices.Add(new USBDeviceInfo(
        (string)device.GetPropertyValue("DeviceID"),
        (string)device.GetPropertyValue("PNPDeviceID"),
        (string)device.GetPropertyValue("Description"),
        (string)device.GetPropertyValue("Manufacturer"),
        (string[])device.GetPropertyValue("HardwareID"),
        (string)device.GetPropertyValue("Caption")
        ));
      }
      collection.Dispose();
      return devices;
    }
  }

  }


发表评论