1

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

我使用 WMI Query Builder 来创建查询的要点:

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 变量,但我得到的只是 Invalid Query 错误。

任何指向正确方向的指针?

让 WMI 查询生成器运行或让我的 LIKE 子句运行将非常有帮助!

4

1 回答 1

3

答案可能需要很长时间,但为了任何仍然感兴趣的人的利益,这是我想出的解决方案。诀窍是利用 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
            }
        }
    }
}
于 2013-07-20T21:50:54.063 回答