C# 获取设备父级的字符串

C# 获取设备父级的字符串,c#,vb.net,windows,C#,Vb.net,Windows,在windows7的设备管理器中,在com端口的分支中,我选择“属性”菜单中的一个端口。在“详细信息”选项卡中,我选择了属性“父项”,并查看字符串: 如何在cmd中从vb.net或visual studio中的其他语言获取此字符串也很好 我试着使用win32_uu类:pnpentity、serialPort等,但这并没有解决我的问题,甚至PS中Get-WMIObject win32_serialPort的输出也没有属性“parent” 除了设备ID,我尝试了标题和中可用的所有语法。 你有什么想

在windows7的设备管理器中,在com端口的分支中,我选择“属性”菜单中的一个端口。在“详细信息”选项卡中,我选择了属性“父项”,并查看字符串:

如何在cmd中从vb.net或visual studio中的其他语言获取此字符串也很好

我试着使用win32_uu类:pnpentity、serialPort等,但这并没有解决我的问题,甚至PS中Get-WMIObject win32_serialPort的输出也没有属性“parent”

除了设备ID,我尝试了标题和中可用的所有语法。
你有什么想法吗?

我的解决方案如下:

  • 使用PnPDeviceID获取所有活动端口:

    private static List<PortInfo> GetActivePorts()
    {
        var ports = new List<PortInfo>();
    
        using (var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_SerialPort"))
        using (var collection = searcher.Get())
        {
            foreach (var device in collection)
            {
                var portInfo = new PortInfo
                {
                    Port = (string)device.GetPropertyValue("DeviceID"),
                    PnPDeviceId = (string)device.GetPropertyValue("PNPDeviceID")
                };
                if (!string.IsNullOrEmpty(portInfo.Port) && !string.IsNullOrEmpty(portInfo.PnPDeviceId))
                {
                    ports.Add(portInfo);
                }
            }
        }
    
        return ports;
    }
    
  • 填充ParentDeviceID:

    private static async void FillParentIds(IReadOnlyCollection<PortInfo> ports)
    {
        var propertiesToQuery = new List<string> {
            "System.Devices.DeviceInstanceId",
            "System.Devices.Parent"
        };
    
        var aqs = string.Join(" OR ", ports.Select(p => $"System.Devices.DeviceInstanceId:={p.PnPDeviceId}"));
        var pnpDevices = await PnpObject.FindAllAsync(PnpObjectType.Device, propertiesToQuery, aqs);
        foreach (var pnpDevice in pnpDevices)
        {
            var port = ports.FirstOrDefault(p => string.Compare(p.PnPDeviceId, pnpDevice.Id, StringComparison.InvariantCultureIgnoreCase) == 0);
            if (port != null && pnpDevice.Properties.TryGetValue("System.Devices.Parent", out var parentId))
            {
                port.ParentDeviceId = parentId?.ToString();
            }
        }
    }
    
    私有静态异步void FillParentId(IReadOnlyCollection端口)
    {
    var propertiesToQuery=新列表{
    “System.Devices.DeviceInstanceId”,
    “System.Devices.Parent”
    };
    var aqs=string.Join(“OR”,ports.Select(p=>$“System.Devices.DeviceInstanceId:={p.PnPDeviceId}”);
    var pnpDevices=await PnpObject.findalsync(PnpObjectType.Device,propertiesToQuery,aqs);
    foreach(pnpDevices中的var pnpDevice)
    {
    var port=ports.FirstOrDefault(p=>string.Compare(p.PnPDeviceId、pnpDevice.Id、StringComparison.InvariantCultureIgnoreCase)==0);
    if(port!=null&&pnpDevice.Properties.TryGetValue(“System.Devices.Parent”,out var parentId))
    {
    port.ParentDeviceId=parentId?.ToString();
    }
    }
    }
    

  • ParentDeviceId将是您要查找的字符串。

    我的解决方案如下:

  • 使用PnPDeviceID获取所有活动端口:

    private static List<PortInfo> GetActivePorts()
    {
        var ports = new List<PortInfo>();
    
        using (var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_SerialPort"))
        using (var collection = searcher.Get())
        {
            foreach (var device in collection)
            {
                var portInfo = new PortInfo
                {
                    Port = (string)device.GetPropertyValue("DeviceID"),
                    PnPDeviceId = (string)device.GetPropertyValue("PNPDeviceID")
                };
                if (!string.IsNullOrEmpty(portInfo.Port) && !string.IsNullOrEmpty(portInfo.PnPDeviceId))
                {
                    ports.Add(portInfo);
                }
            }
        }
    
        return ports;
    }
    
  • 填充ParentDeviceID:

    private static async void FillParentIds(IReadOnlyCollection<PortInfo> ports)
    {
        var propertiesToQuery = new List<string> {
            "System.Devices.DeviceInstanceId",
            "System.Devices.Parent"
        };
    
        var aqs = string.Join(" OR ", ports.Select(p => $"System.Devices.DeviceInstanceId:={p.PnPDeviceId}"));
        var pnpDevices = await PnpObject.FindAllAsync(PnpObjectType.Device, propertiesToQuery, aqs);
        foreach (var pnpDevice in pnpDevices)
        {
            var port = ports.FirstOrDefault(p => string.Compare(p.PnPDeviceId, pnpDevice.Id, StringComparison.InvariantCultureIgnoreCase) == 0);
            if (port != null && pnpDevice.Properties.TryGetValue("System.Devices.Parent", out var parentId))
            {
                port.ParentDeviceId = parentId?.ToString();
            }
        }
    }
    
    私有静态异步void FillParentId(IReadOnlyCollection端口)
    {
    var propertiesToQuery=新列表{
    “System.Devices.DeviceInstanceId”,
    “System.Devices.Parent”
    };
    var aqs=string.Join(“OR”,ports.Select(p=>$“System.Devices.DeviceInstanceId:={p.PnPDeviceId}”);
    var pnpDevices=await PnpObject.findalsync(PnpObjectType.Device,propertiesToQuery,aqs);
    foreach(pnpDevices中的var pnpDevice)
    {
    var port=ports.FirstOrDefault(p=>string.Compare(p.PnPDeviceId、pnpDevice.Id、StringComparison.InvariantCultureIgnoreCase)==0);
    if(port!=null&&pnpDevice.Properties.TryGetValue(“System.Devices.Parent”,out var parentId))
    {
    port.ParentDeviceId=parentId?.ToString();
    }
    }
    }
    

  • ParentDeviceId将是您要查找的字符串。

    您用C#或Vb编写的是哪种语言?还有,到目前为止你尝试了什么?看看是否对你有帮助我使用Vb,但这并不重要,因为我确信在我的例子中,c#上的解决方案将是类似的。你看过我的课文了吗?我写道,我使用Win32_SerialPort等尝试获取设备的父级。请参阅主消息中的代码。Pikoh,谢谢!我会试着用这个。你用C#或Vb写的是哪种语言?还有,到目前为止你尝试了什么?看看是否对你有帮助我使用Vb,但这并不重要,因为我确信在我的例子中,c#上的解决方案将是类似的。你看过我的课文了吗?我写道,我使用Win32_SerialPort等尝试获取设备的父级。请参阅主消息中的代码。Pikoh,谢谢!我会试着用这个。米哈斯,非常感谢你的回复。我会尽快尝试并与您分享结果。WINDOWS。DEVICE.枚举。PNP.DLL,有没有其他的创建PNPOBEKET的解决方案?您可以在Windows.Foundation.UniversalApiContract.winmd找到它,它是Windows 10 SDK()MiHass的一部分,非常感谢您的响应。我会尽快尝试并与你分享结果。WINDOWS。DEVICE.枚举。PNP.DLL,有没有其他的创建PNPOBEKET的解决方案?你可以在Windows.Foundation.UniversalApiContract.winmd找到它,它是Windows 10 SDK()的一部分。