【问题标题】:Get the string with parent of device获取设备父级的字符串
【发布时间】:2017-03-11 02:03:40
【问题描述】:

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

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

我尝试使用 win32_ 类:pnpentity、serialPort 等,但这并没有解决我的问题,即使 PS 中 Get-WMIObject Win32_SerialPort 的输出没有属性“父”。

 Dim objService = GetObject("winmgmts://./root/cimv2")

        For Each objPort In objService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE ClassGuid='{4d36e978-e325-11ce-bfc1-08002be10318}'")

            Console.WriteLine(objPort.Caption & vbCrLf)

            Console.Write(objPort.DeviceID & vbCrLf)
            Console.ReadLine()

        Next

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

【问题讨论】:

  • 您用 C# 还是 Vb 编写哪种语言?到目前为止,您还尝试过什么?
  • 看看this answer对你有没有帮助
  • 我使用 Vb,但这没关系,因为我确信在我的情况下,c# 上的解决方案将是相似的。你读过我的文字吗?我写道,我使用 Win32_SerialPort 等来尝试获取设备的父级。请参阅主消息中的代码。
  • 皮科,谢谢!我会尝试使用它。

标签: c# vb.net windows


【解决方案1】:

我的解决方案如下:

  1. 获取所有活动端口及其 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;
    }
    

PortInfo 在哪里

    private class PortInfo
    {
        public string Port { get; set; }
        public string PnPDeviceId { get; set; }
        public string ParentDeviceId { get; set; }
    }
  1. 填写 ParentDeviceIds :

    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();
            }
        }
    }
    

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

【讨论】:

  • 米哈斯,非常感谢您的回复。我会尽快尝试并与您分享结果。
  • Windows.Devices.Enumeration.pnp.dll 在哪里,是否有任何替代解决方案来创建 PnpObject?
  • 您可以在 Windows.Foundation.UniversalApiContract.winmd 中找到它,它是 Windows 10 SDK (developer.microsoft.com/en-us/windows/downloads/windows-10-sdk) 的一部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
相关资源
最近更新 更多