简体   繁体   English

如何在WMI Win32_Directory查询的目录名称中转义右括号(})?

[英]How can I escape a right-curly brace ( } ) in a directory name of a WMI Win32_Directory query?

I'm trying to execute a WMI query that lists all the subdirectories in a directory on a different network-attached computer (server), but it's failing with a System.Management.ManagementException "Invalid query" exception. 我正在尝试执行一个WMI查询,该查询列出了另一台连接网络的计算机(服务器)上目录中的所有子目录,但是由于System.Management.ManagementException “无效查询”异常而失败。

I am practically certain that this issue is due to the directory name containing left- and/or right-curly braces ( { , } ). 我实际上可以确定,此问题是由于目录名称包含左括号和/或右括号( {} )引起的。

UPDATE: I've determined that the issue is not the left-curly brace, but the right-curly brace. 更新:我已经确定问题不是左花括号,而是右花括号。

I've tried various things to escape these characters, but nothing seems to work. 我已经尝试了各种方法来逃避这些字符,但是似乎没有任何效果。

Below is a contrived example that fails with "Invalid query" every time. 下面是一个人为设计的示例,每次都会以“无效查询”失败。

using System;
using System.Management;

public static class Program
{
    public static void Main()
    {
        const string username = @"Domain\User";
        const string password = @"Password";
        const string server = @"Server";

        const string query = @"Associators of {"
                             + @"Win32_Directory.Name='"
                             + @"c:\program files (x86)\a_test}"
                             + @"'} "
                             + @"Where AssocClass = Win32_Subdirectory ResultRole = PartComponent";

        var options = new ConnectionOptions { Username = username, Password = password };
        var wmiScope = new ManagementScope(@"\\" + server + @"\root\cimv2", options);
        var wmiQuery = new ObjectQuery(query);
        var searcher = new ManagementObjectSearcher(wmiScope, wmiQuery);
        var searchResults = searcher.Get();

        foreach (var searchResult in searchResults)
        {
            var subPath = searchResult.GetPropertyValue("Name").ToString();
            var system = Convert.ToBoolean(searchResult.GetPropertyValue("System"));
            Console.WriteLine($"subPath = {subPath}; system = {system}");
        }
    }
}

For what it's worth, the code is running on a Windows 10 machine querying a Windows 2008 R2 SP1 server (yes, it's scheduled for demolition). 值得一试的是,该代码正在Windows 10计算机上运行,​​查询Windows 2008 R2 SP1服务器(是的,已计划拆除)。

Thanks! 谢谢!

This appears to be a quirk of the Associators of parsing. 这似乎是解析Associators of的怪癖。 To fix this, use the double quote syntax for strings rather than the single quote syntax (taking care to escape backslashes, since this is required for the double quote approach): 要解决此问题,请对字符串使用双引号语法,而不要使用单引号语法(请注意避免转义反斜杠,因为双引号方法需要这样做):

const string query = @"Associators of {"
                     + @"Win32_Directory.Name="""
                     + @"c:\\program files (x86)\\a_test}"
                     + @"""} "
                     + @"Where AssocClass = Win32_Subdirectory ResultRole = PartComponent";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM