USBデバイスから位置情報を取得する

USB ハブには 20 個のポートがあり、各ポートにはラベルが付いているので、Android デバイスに X という名前のラベルが挿入されている場合、これを取得する方法 Hub_#0003 の文字列がテーブルに対応します。その後、もう一度確認してください。

ID 位置
1 ポート_#0002.ハブ_#0003 
2 ポート_#0002.ハブ_#0001 
3 ポート_#0002.ハブ_#0006 
4 ポート_#0001.ハブ_#0006 
5 ポート_#0002.ハブ_#0007 
6 ポート_#0002.ハブ_#0002 
7 ポート_#0002.ハブ_#0005 

public string GetDeviceLocation(string deviceID)
    {         string result = "";         var searchers = new ManagementObjectSearcher(null, "SELECT * FROM Win32_PnPEntity where DeviceID LIKE 'USB%'").Get().OfType<ManagementObject>();         int devicdNum = 0;         デバイスID = デバイスID.ToLower();         foreach (サーチャー内の var mo)         {             devicdNum++;             // 2 つのプロパティを要求します







            var args = new object[] { new string[] { "DEVPKEY_Device_FriendlyName", "DEVPKEY_Device_Parent", "DEVPKEY_Device_LocationInfo", "DEVPKEY_Device_LocationPaths", "DEVPKEY_Device_InstanceId" }, null };

            // Win32_PnPEntity.GetDeviceProperties を呼び出します
            mo.InvokeMethod("GetDeviceProperties", args);

            var mbos = (ManagementBaseObject[])args[1]; // デバイスのプロパティ キーごとに 1 つの mbo

            var name = mbos[0].Properties.OfType<PropertyData>().FirstOrDefault(p => p.Name == "データ")?.Value;
            varparent = mbos[1].Properties.OfType<PropertyData>().FirstOrDefault(p => p.Name == "データ")?.Value;
            var location = mbos[2].Properties.OfType<PropertyData>().FirstOrDefault(p => p.Name == "データ")?.Value;
            var Device_InstanceId = mbos[4].Properties.OfType<PropertyData>().FirstOrDefault(p => p.Name == "データ")?.Value;
            bool bIsDeviceWeWant = false;
           
            if (Device_InstanceId != null && 位置 != null && 親 != null)
            {

                if (
                    Device_InstanceId.ToString().ToLower().IndexOf(deviceID) != -1
                    )
                {                     bIsDeviceWeWant = true;                 }

            }

            if (bIsDeviceWeWant == true)
            {                 if (name == null) name = "FriendlyName_NULL";                 if (name.ToString().IndexOf("ADB") != -1) 続行;                 var locationPath = mbos[3].Properties.OfType<PropertyData>().FirstOrDefault(p => p.Name == "データ")?.Value;


                if (locationPath != null && ((string[])locationPath).Length == 2)
                {                     result = location.ToString();                     add_log(PadRightWith Chinese(name.ToString(), 30) + PadRightWith Chinese(Device_InstanceId.ToString(), 45) +                         " " + PadRightWith Chinese(parent.ToString(), 40) + " " + PadRightWith Chinese(location.ToString(), 50) ) +                         " " + PadRightWith Chinese(((string[])locationPath)[0], 60) + " " + PadRightWith Chinese(((string[])locationPath)[1], 64));                     コンソール。WriteLine(名前 + " " + 親 + " " + 場所 + " " + ((string[])locationPath)[0] + " " + ((string[])locationPath)[1]);                 }






                if (locationPath != null && ((string[])locationPath).Length == 1)
                { 
                }
            }
        }
        結果を返します。
    }

おすすめ

転載: blog.csdn.net/babytiger/article/details/132906618
おすすめ