WQL语法:带有LIKE运算符的DiskDriveToDiskPartition

时间:2012-04-05 09:23:27

标签: c# syntax wql

所以我试图将物理驱动器与分区匹配以驱动字母,DiskDriveToDiskPartition似乎是这样做的一个很好的候选者,但我很难让查询像我想要的那样工作:

我使用WMI查询生成器来创建查询的要点:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", 
    "SELECT * FROM Win32_DiskDriveToDiskPartition WHERE 
    Antecedent = \\\\localhost\\root\\cimv2:Win32_DiskDrive.DeviceID="\\\\\\\\.\\\\PHYSICALDRIVE3""); 

对于初学者,Visual Studio告诉我这不是一个有效的查询,因为它中有太多\字符,还有需要排序的非法引号。其次,我想简单地读一下WHERE子句

WHERE Antecedent LIKE \"%" + physicalDriveID + "%\" 

想法是传递一个PHYSICALDRIVE变量,但我得到的是无效查询错误。

这个方向的正确方向是什么?

让WMI Query Builder运行或让我的LIKE子句运行会非常有用!

1 个答案:

答案 0 :(得分:2)

答案可能是很长一段时间,但为了任何仍然感兴趣的人的利益,这是我提出的解决方案。诀窍是使用WMI查询的 ASSOCIATORS OF 语法。这样我们就可以有效地将DeviceID加入分区。

using (ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"))
{
    // extract model and interface information
    foreach (ManagementObject drive in search.Get())
    {
        string antecedent = drive["DeviceID"].ToString(); // the disk we're trying to find out about
        antecedent = antecedent.Replace(@"\", "\\"); // this is just to escape the slashes
        string query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + antecedent + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition";
        using (ManagementObjectSearcher partitionSearch = new ManagementObjectSearcher(query))
        {
            foreach (ManagementObject part in partitionSearch.Get())
            {
                //...pull out the partition information
            }
        }
    }
}
相关问题