微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 获取有关Windows和NET中已连接USB设备的所有可用信息

我已经在我的代码中实现了 SO question解决方案,但我正在寻找来自连接的USB设备的更多信息.

我注意到在我的设备管理器中提供了更多的信息.

enter image description here

我特别想知道这些设备的制造商.

我不确定如何确定从SO question中看到的GetPropertyValue方法使用中可用的其他属性.我在最后尝试了一些关键字,但它们都报告错误,所以我认为这不是可用的属性.

知道如何获取更多只有deviceid,Pnpdeviceid和Description的信息吗?

编辑:对于任何想知道这里的人来说,我会得到完整的属性列表和值.没有任何设备可以提供比我所知道的更多或更少(可能是转换为字符串?).

Availability: 
                    Caption: USB Root Hub
                  ClassCode: 
     ConfigManagerErrorCode: 0
    ConfigManagerUserConfig: False
          CreationClassName: Win32_USBHub
   CurrentAlternateSettings: 
         CurrentConfigValue: 
                Description: USB Root Hub
                   deviceid: USB\ROOT_HUB20\########
               ErrorCleared: 
           ErrorDescription: 
               GangSwitched: 
                InstallDate: 
              LastErrorCode: 
                       Name: USB Root Hub
            NumberOfConfigs: 
              NumberOfPorts: 
                PNPdeviceid: USB\ROOT_HUB20\4&\########&0
PowerManagementCapabilities: 
   PowerManagementSupported: 
               ProtocolCode: 
                     Status: OK
                 StatusInfo: 
               SubclassCode: 
    SystemCreationClassName: Win32_ComputerSystem
                 SystemName: ASystemName
                 USBVersion:

并且编辑了来自链接的SO答案的代码,但具有所有属性.

public class USBDeviceInfo
{
    public String Availability { get; set; }
    public String Caption { get; set; }
    public String ClassCode { get; set; }
    public UInt32 ConfigManagerErrorCode { get; set; }
    public Boolean ConfigManagerUserConfig { get; set; }
    public String CreationClassName { get; set; }
    public String CurrentAlternateSettings { get; set; }
    public String CurrentConfigValue { get; set; }
    public String Description { get; set; }
    public String deviceid { get; set; }
    public String ErrorCleared { get; set; }
    public String ErrorDescription { get; set; }
    public String GangSwitched { get; set; }
    public String InstallDate { get; set; }
    public String LastErrorCode { get; set; }
    public String Name { get; set; }
    public String NumberOfConfigs { get; set; }
    public String NumberOfPorts { get; set; }
    public String PNPdeviceid { get; set; }
    public String PowerManagementCapabilities { get; set; }
    public String PowerManagementSupported { get; set; }
    public String ProtocolCode { get; set; }
    public String Status { get; set; }
    public String StatusInfo { get; set; }
    public String SubclassCode { get; set; }
    public String SystemCreationClassName { get; set; }
    public String SystemName { get; set; }
    public String USBVersion { get; set; }
}

public static List<USBDeviceInfo> GetUSBDevices()
{
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub");
    ManagementObjectCollection collection = searcher.Get();

    List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
    foreach (var device in collection)
    {
        USBDeviceInfo deviceInfo = new USBDeviceInfo();
        deviceInfo.Availability = (String)device.GetPropertyValue("Availability");
        deviceInfo.Caption = (String)device.GetPropertyValue("Caption");
        deviceInfo.ClassCode = (String)device.GetPropertyValue("ClassCode");
        deviceInfo.ConfigManagerErrorCode = (UInt32)device.GetPropertyValue("ConfigManagerErrorCode");
        deviceInfo.ConfigManagerUserConfig = (Boolean)device.GetPropertyValue("ConfigManagerUserConfig");
        deviceInfo.CreationClassName = (String)device.GetPropertyValue("CreationClassName");
        deviceInfo.CurrentAlternateSettings = (String)device.GetPropertyValue("CurrentAlternateSettings");
        deviceInfo.CurrentConfigValue = (String)device.GetPropertyValue("CurrentConfigValue");
        deviceInfo.Description = (String)device.GetPropertyValue("Description");
        deviceInfo.deviceid = (String)device.GetPropertyValue("deviceid");
        deviceInfo.ErrorCleared = (String)device.GetPropertyValue("ErrorCleared");
        deviceInfo.ErrorDescription = (String)device.GetPropertyValue("ErrorDescription");
        deviceInfo.GangSwitched = (String)device.GetPropertyValue("GangSwitched");
        deviceInfo.InstallDate = (String)device.GetPropertyValue("InstallDate");
        deviceInfo.LastErrorCode = (String)device.GetPropertyValue("LastErrorCode");
        deviceInfo.Name = (String)device.GetPropertyValue("Name");
        deviceInfo.NumberOfConfigs = (String)device.GetPropertyValue("NumberOfConfigs");
        deviceInfo.NumberOfPorts = (String)device.GetPropertyValue("NumberOfPorts");
        deviceInfo.PNPdeviceid = (String)device.GetPropertyValue("PNPdeviceid");
        deviceInfo.PowerManagementCapabilities = (String)device.GetPropertyValue("PowerManagementCapabilities");
        deviceInfo.PowerManagementSupported = (String)device.GetPropertyValue("PowerManagementSupported");
        deviceInfo.ProtocolCode = (String)device.GetPropertyValue("ProtocolCode");
        deviceInfo.Status = (String)device.GetPropertyValue("Status");
        deviceInfo.StatusInfo = (String)device.GetPropertyValue("StatusInfo");
        deviceInfo.SubclassCode = (String)device.GetPropertyValue("SubclassCode");
        deviceInfo.SystemCreationClassName = (String)device.GetPropertyValue("SystemCreationClassName");
        deviceInfo.SystemName = (String)device.GetPropertyValue("SystemName");
        deviceInfo.USBVersion = (String)device.GetPropertyValue("USBVersion");
        devices.Add(deviceInfo);
    }

    collection.dispose();
    searcher.dispose();
    return devices;
}

解决方法

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐