首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从DEVPKEY_Device_BusReportedDeviceDesc获取Win32_PnPEntity in c#

从DEVPKEY_Device_BusReportedDeviceDesc获取Win32_PnPEntity in c#
EN

Stack Overflow用户
提问于 2021-09-28 13:42:41
回答 1查看 545关注 0票数 2

我正在尝试从我们的WPF应用程序中的usb端口获取总线报告的设备描述。找到了一个powershell脚本,它给出了非常精确的结果,但并不是每个用户都有运行powershell的权限,因此遗憾的是,使用PowerShell.Create()并不是一种选择。

代码语言:javascript
运行
复制
(Get-WMIObject Win32_PnPEntity | where {$_.name -match "\(com*"}).GetDeviceProperties("DEVPKEY_Device_BusReportedDeviceDesc").DeviceProperties.Data

我尝试了下面的组合,但是我要么无法将'System.String‘类型的对象转换为’System.Array‘,要么在InvokeMethod上得到“无效的方法参数”。

代码语言:javascript
运行
复制
using System;
using System.Management;

ManagementClass oClass = new ManagementClass("Win32_PnPEntity");

Object[] single = { "DEVPKEY_Device_BusReportedDeviceDesc" };
Array arr = (Array)new string[] { "DEVPKEY_Device_BusReportedDeviceDesc" };
Object[] singleArr = { single };
Object[] objsArr = { arr };
ManagementBaseObject inParams = oClass.GetMethodParameters("GetDeviceProperties");
inParams["devicePropertyKeys"] = single;
//inParams["devicePropertyKeys"] = arr;
//inParams["devicePropertyKeys"] = singleArr;
//inParams["devicePropertyKeys"] = objsArr;

oClass.InvokeMethod("GetDeviceProperties", single);
//oClass.InvokeMethod("GetDeviceProperties", singleArr);
//oClass.InvokeMethod("GetDeviceProperties", objsArr);
//oClass.InvokeMethod("GetDeviceProperties", inParams, null);

一切都发生在本地机器上。

还从vromanov 如何使用C#获取总线报告的设备描述找到了一个答案,它很有效,但似乎是一个过火的解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-28 14:33:53

GetDeviceProperties方法如下所示:

代码语言:javascript
运行
复制
Uint32 GetDeviceProperties(
  [in, optional] string                  devicePropertyKeys[],
  [out]          Win32_PnPDeviceProperty deviceProperties[]
);

下面是一个用C#调用它的示例代码:

代码语言:javascript
运行
复制
foreach (var mo in new ManagementObjectSearcher(null, "SELECT * FROM Win32_PnPEntity").Get().OfType<ManagementObject>())
{
    // get the name so we can do some tests on the name in this case
    var name = mo["Name"] as string;

    // add your custom test here
    if (name == null || name.IndexOf("(co", StringComparison.OrdinalIgnoreCase) < 0)
        continue;

    // call Win32_PnPEntity's 'GetDeviceProperties' method
    // prepare two arguments:
    //  1st one is an array of string (or null)
    //  2nd one will be filled on return (it's an array of ManagementBaseObject)
    var args = new object[] { new string[] { "DEVPKEY_Device_BusReportedDeviceDesc" }, null };
    mo.InvokeMethod("GetDeviceProperties", args);

    // one mbo for each device property key
    var mbos = (ManagementBaseObject[])args[1];
    if (mbos.Length > 0)
    {
        // get value of property named "Data"
        // not all objects have that so we enum all props here
        var data = mbos[0].Properties.OfType<PropertyData>().FirstOrDefault(p => p.Name == "Data");
        if (data != null)
        {
            Console.WriteLine(data.Value);
        }
    }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69362886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档