usb设备的定位信息获取

一个usb hub有20个口,每个口有一个标号,那么如果一个为X的标号插入了一个android设备,如何通过函数,得到这个X,代码实现如下,获取了location, 得到类似 Port_#0002.Hub_#0003 的字符串,然后,要对应一个表。然后反查一下就行了

ID 位置
1 Port_#0002.Hub_#0003 
2 Port_#0002.Hub_#0001 
3 Port_#0002.Hub_#0006 
4 Port_#0001.Hub_#0006 
5 Port_#0002.Hub_#0007 
6 Port_#0002.Hub_#0002 
7 Port_#0002.Hub_#0005 

public string GetDeviceLocation(string deviceID)
    {
        string result = "";
        var searchers = new ManagementObjectSearcher(null, "SELECT * FROM Win32_PnPEntity where DeviceID LIKE 'USB%'").Get().OfType();
        int devicdNum = 0;
        deviceID = deviceID.ToLower();
        foreach (var mo in searchers)
        {
            devicdNum++;
            // ask for 2 properties

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

            // call Win32_PnPEntity.GetDeviceProperties
            mo.InvokeMethod("GetDeviceProperties", args);

            var mbos = (ManagementBaseObject[])args[1]; // one mbo for each device property key

            var name = mbos[0].Properties.OfType().FirstOrDefault(p => p.Name == "Data")?.Value;
            var parent = mbos[1].Properties.OfType().FirstOrDefault(p => p.Name == "Data")?.Value;
            var location = mbos[2].Properties.OfType().FirstOrDefault(p => p.Name == "Data")?.Value;
            var Device_InstanceId = mbos[4].Properties.OfType().FirstOrDefault(p => p.Name == "Data")?.Value;
            bool bIsDeviceWeWant = false;
           
            if (Device_InstanceId != null && location != null && parent != 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) continue;
                var locationPath = mbos[3].Properties.OfType().FirstOrDefault(p => p.Name == "Data")?.Value;

                if (locationPath != null && ((string[])locationPath).Length == 2)
                {
                    result = location.ToString();
                    add_log(PadRightWithChinese(name.ToString(), 30) + PadRightWithChinese(Device_InstanceId.ToString(), 45) +
                        " " + PadRightWithChinese(parent.ToString(), 40) + " " + PadRightWithChinese(location.ToString(), 50) +
                        " " + PadRightWithChinese(((string[])locationPath)[0], 60) + " " + PadRightWithChinese(((string[])locationPath)[1], 64));
                    Console.WriteLine(name + "    " + parent + "     " + location + "    " + ((string[])locationPath)[0] + "     " + ((string[])locationPath)[1]);
                }
                if (locationPath != null && ((string[])locationPath).Length == 1)
                { 
                }
            }
        }
        return result;
    }

你可能感兴趣的:(usb,定位)