9

システムで現在使用されている USB デバイスの種類を知る必要があります。USB デバイスのクラスコードに関するUSB 仕様があります。しかし、デバイス タイプを取得できません。WMI 要求WQL: select * from Win32_UsbHubで、クラス コード、サブクラス コード、プロトコル タイプ フィールドに null 値が返されます。現在使用中の USB デバイスの種類を検出する方法はありますか?

私の現在のコード:

ManagementObjectCollection collection; 
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub")) 
{
    collection = searcher.Get();
    foreach (var device in collection)
        {
            var deviceId = (string)GetPropertyValue("DeviceID");
            var pnpDeviceId = (string)GetPropertyValue("PNPDeviceID");
            var descr = (string)device.GetPropertyValue("Description");
            var classCode = device.GetPropertyValue("ClassCode"); //null here
        }
}
4

1 に答える 1

5

開始点としてUSB View Sourceをダウンロードできます。これは、PC (C#) 上のすべての USB デバイスをループし、それぞれの情報を取得します。Class codeSubclass code、およびtype フィールドを取得するには、Protocol少し変更する必要があります。以下を変更して実行すると、ツリー ビューの項目をクリックすると、各 USB デバイスの情報が表示されます (情報は右側のパネルに表示されます)。

USB.cs の変更:

// Add the following properties to the USBDevice class
// Leave everything else as is
public byte DeviceClass
{
   get { return DeviceDescriptor.bDeviceClass; }
}

public byte DeviceSubClass
{
   get { return DeviceDescriptor.bDeviceSubClass; }
}

public byte DeviceProtocol
{
   get { return DeviceDescriptor.bDeviceProtocol; }
}

fmMain.cs の変更

// Add the following lines inside the ProcessHub function
// inside the "if (port.IsDeviceConnected)" statement
// Leave everything else as is
if (port.IsDeviceConnected)
{
   // ...
   sb.AppendLine("SerialNumber=" + device.SerialNumber);
   // Add these three lines
   sb.AppendLine("DeviceClass=0x" + device.DeviceClass.ToString("X"));
   sb.AppendLine("DeviceSubClass=0x" + device.DeviceSubClass.ToString("X"));
   sb.AppendLine("DeviceProtocol=0x" + device.DeviceProtocol.ToString("X"));
   // ...
}
于 2013-08-16T13:21:41.347 に答える