WQL语法:具有LIKE运算符的DiskDriveToDiskPartition [英] WQL syntax: DiskDriveToDiskPartition with a LIKE operator

查看:176
本文介绍了WQL语法:具有LIKE运算符的DiskDriveToDiskPartition的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图将物理驱动器与分区与驱动器号进行匹配,DiskDriveToDiskPartition似乎是这样做的不错选择,但我正在努力使查询正常运行,如我所愿:

So I'm trying to match up physical drives to partitions to drive letters, and DiskDriveToDiskPartition seems like a good candidate for doing that, but I'm struggling to get the query to work like I want it to:

我已经使用WMI查询生成器创建查询的依据:

I've used the WMI Query Builder to create the gist of the query:

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

对于初学者来说,Visual Studio告诉我这不是一个有效的查询,因为其中包含太多\字符,此外还需要对非法引号进行排序.其次,我只想简单地在WHERE子句中阅读

For starters, Visual Studio tells me that this isn't a valid query as it has too many \ characters in it, plus there are illegal quotes that need to be sorted. Secondly, I'd like to simply the WHERE clause to just read

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

将其传递给PHYSICALDRIVE变量的想法是,但是我得到的只是无效的查询错误.

The idea being to pass it a PHYSICALDRIVE variable, but all I get is Invalid Query errors.

在这个方向上是否有正确的指针?

Any pointers in the right direction on this one?

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

Either getting the WMI Query Builder to run or getting my LIKE clause to run would be really helpful!

推荐答案

答案可能很长一段时间了,但是对于仍然有兴趣的人来说,这是我想出的解决方案.诀窍是利用WMI查询的 ASSOCIATORS OF 语法.这样,我们可以有效地将DeviceID联接到分区.

An answer is probably a long time coming but for the benefit of anyone still interested this was the solution I came up with. The trick is to make use of the ASSOCIATORS OF syntax of the WMI query. This way we can effectively JOIN the DeviceID to partitions.

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
            }
        }
    }
}

这篇关于WQL语法:具有LIKE运算符的DiskDriveToDiskPartition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆