我正在嘗試從 WPF 應用程式中的 USB com 埠獲取總線報告的設備描述。找到了一個 powershell 腳本,它給出了確切的答案,但由于并非每個用戶都有權運行 powershell,因此遺憾的是,使用 PowerShell.Create() 不是一種選擇。
(Get-WMIObject Win32_PnPEntity | where {$_.name -match "\(com*"}).GetDeviceProperties("DEVPKEY_Device_BusReportedDeviceDesc").DeviceProperties.Data
我嘗試了以下組合,但我在 InvokeMethod 上得到“無法將型別為 'System.String' 的物件強制轉換為型別為 'System.Array'”或“無效的方法引數”。
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 How to get Bus Reported Device Description using C#找到了一個答案,它可以作業,但似乎是一個矯枉過正的解決方案
uj5u.com熱心網友回復:
該GetDeviceProperties方法進行了說明如下:
Uint32 GetDeviceProperties(
[in, optional] string devicePropertyKeys[],
[out] Win32_PnPDeviceProperty deviceProperties[]
);
所以這里有一個示例代碼,可以用 C# 呼叫它:
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);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/314744.html
下一篇:確定MFT是GPU還是CPU?
