Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

c# - Getting MSFT_Partition from MSFT_Disk using Associators

I am trying to get a list of MSFT_Partitions from a MSFT_Disk object and loop over them. This is the code I have been using so far but it always outputs an System.Management.ManagementException: 'Invalid query ' exception.

This is the code I am using at the moment:

public static void GetDiskInfo() {
    var rawDiskInfos = new ManagementObjectSearcher("root\Microsoft\Windows\Storage", "SELECT * FROM MSFT_Disk");
    foreach(var rawDiskInfo in rawDiskInfos.Get()) {
        Console.WriteLine(rawDiskInfo["FriendlyName"]);
        GetPartitionInfo(rawDiskInfo["ObjectId"]);
    }
}

public static void GetPartitionInfo(object objectId) {
    ManagementScope scope = new ManagementScope("\\.\ROOT\Microsoft\Windows\Storage");

    var query = new ObjectQuery("ASSOCIATORS OF {MSFT_Disk.ObjectId="" + objectId + ""} WHERE AssocClass = MSFT_DiskToPartition");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    ManagementObjectCollection queryCollection = searcher.Get();

    foreach(var partiton in queryCollection) {
        Console.WriteLine(partiton["Guid"]);
    }
}

I already tried a lot of solutions I found online and all of them resulted in the same exception.

Thank you very much for your help!

question from:https://stackoverflow.com/questions/65853603/getting-msft-partition-from-msft-disk-using-associators

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Most queries you'll find on the internet don't do it, but in the general case, you must escape raw strings you pass to WQL queries (in your case objectId contains special characters), using the backslash character, with a method like this:

public static string EscapeWql(string text)
{
    if (text == null)
        return null;

    var sb = new StringBuilder(text.Length);
    foreach (var c in text)
    {
        if (c == '\' || c == ''' || c == '"')
        {
            sb.Append('\');
        }
        sb.Append(c);
    }
    return sb.ToString();
}

So your method should now look like this:

public static void GetPartitionInfo(object objectId)
{
    var scope = new ManagementScope(@"rootMicrosoftWindowsStorage");
    var query = new ObjectQuery("ASSOCIATORS OF {MSFT_Disk.ObjectId="" + EscapeWql((string)objectId) + ""} WHERE AssocClass = MSFT_DiskToPartition");
    using (var searcher = new ManagementObjectSearcher(scope, query))
    {
        using (var queryCollection = searcher.Get())
        {
            foreach (var partition in queryCollection)
            {
                Console.WriteLine(partition["Guid"]);
            }
        }
    }
}

PS: don't forget using statements on IDisposable classes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...