为什么 WMI 通过搜索而不是直接工作?

Why does WMI work through a search but not directly?

这 2 个中的第一个锁定 BitLocker 驱动器。第二个 InvokeMethod 抛出:'Invalid object path'。为什么?他们看起来是等价的。

//Using a search:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\CIMV2\Security\MicrosoftVolumeEncryption", "SELECT * FROM Win32_EncryptableVolume WHERE DriveLetter = 'E:'");
foreach (ManagementObject classInstance1 in searcher.Get())
    classInstance1.InvokeMethod("Lock", new object[] { true }); 

//Direct:
ManagementObject classInstance2 = new ManagementObject("root\CIMV2\Security\MicrosoftVolumeEncryption", "Win32_EncryptableVolume.DriveLetter='E:'", null);
classInstance2.InvokeMethod("Lock", new object[] { 0 });//throws: 'Invalid object path'.

您似乎没有调用 Get() 方法。试试这个:

ManagementObject classInstance2 = new ManagementObject("root\CIMV2\Security\MicrosoftVolumeEncryption", "Win32_EncryptableVolume.DriveLetter='E:'", null);
classInstance2.Get();
classInstance2.InvokeMethod("Lock", new object[] { 0 });

查看此文档:https://docs.microsoft.com/en-us/windows/desktop/wmisdk/retrieving-an-instance

不幸的是,您不能使用不是键 属性 的 属性 来实例化对象。 WMI 中的键 属性 是具有 CIM_Key qualifier, the WMI documentation goes into further detail about the Key Qualifier. For more information about the WMI requirement of using a full path with key to reference an object you can read the WMI documentation about Instance Object Paths 的 属性。

在 C# 中,对于您指定的特定 class (Win32_EncryptableVolume),您只能通过使用示例中所示的 ManagementObjectSearcher 来完成您尝试做的事情。这是因为您正在尝试获取基于标准 属性 而不是密钥 属性.

的实例

WMI Explorer 2.0 是探索 WMI 的一个很好的工具。这给出了 WMI classes 的一个很好的视觉表示。在此实用程序中,关键属性用星号标识。

https://github.com/vinaypamnani/wmie2/releases

我假设正确答案与其他人提到的相似,但不完全相同。

The class's page 提到 DeviceID 有以下 属性:

Qualifiers: Key

我假设,由于缺乏实际文档,通过他们的 Key returns 搜索东西本身。在通过其他方式搜索 returns 满足该条件的对象列表时。即使列表仅包含 1 个条目 - 它不是对象本身,而是一个列表。

但是,如果有人可以提供一些文档,那就太好了。