【问题标题】:Finding wrong escape of WMI query查找 WMI 查询的错误转义
【发布时间】:2018-06-22 09:44:26
【问题描述】:

我在某处对 wmi 查询进行了错误的转义,有人可以帮忙修复它吗?因为它显示无效查询

string deviceid = "Disk #0, Partition #0";
        string antecedent = @"\\" + Environment.MachineName + "\\root\\cimv2:Win32_DiskPartition.DeviceID=\"" + deviceid + "\"";
        ManagementObjectSearcher assosiaciation_query2 = new ManagementObjectSearcher("select Dependent from Win32_LogicalDiskToPartition where Antecedent = \"" + antecedent + "\"");
        foreach (ManagementObject assosiaciation_query_data2 in assosiaciation_query2.Get())
        {
            Console.WriteLine("Dependent: " + assosiaciation_query_data2["Dependent"]);
        }

谢谢。

【问题讨论】:

  • 使用 VisualStudio 调试器,它将帮助您找到错误所在...我们不是调试服务

标签: c# wmi-query


【解决方案1】:

好的,因为在 mysql 中你可以使用 LIKE 来查找类似的表,我还读到 https://msdn.microsoft.com/en-us/library/aa392263%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 在 wmi 查询中使用 LIKE 是可能的

string deviceid = "Disk #0, Partition #0"; ManagementObjectSearcher assosiaciation_query2 = new ManagementObjectSearcher("select Dependent from Win32_LogicalDiskToPartition where Antecedent LIKE '%" + deviceid + "%'");

所以 ant 仍然显示查询无效

【讨论】:

    【解决方案2】:

    您将 2 个双引号放在另外 2 个双引号中,这破坏了您的代码。您应该在先行字符串中使用 WMI 转义码:

    string antecedent = @"\\" + Environment.MachineName + "\\root\\cimv2:Win32_DiskPartition.DeviceID=\\\"" + deviceid + "\\\"";
    

    当 WMI 收到命令时,它应该是这样的:

    \"你的设备ID\"

    插入 2 个顶点应该可以正常工作 :) 示例:

    Mycommand"myOtherCommand\"myvalue\""
    

    编辑: 我完全误解了您的目标,抱歉,您的查询中有错误,因为 '#' 是 MySQL 通配符,会弄乱您的查询(是 MySql 内联注释), 不,即使我使用 where Ascendent like 'aaa',它也不起作用,我想知道为什么你的 where 条件会弄乱代码:/。

    如果您使用以下代码,它可以工作:

        string query = "select * from Win32_LogicalDiskToPartition";
        string antecedent = @"\\" + Environment.MachineName + "\\root\\cimv2:Win32_DiskPartition.DeviceID=\"" + deviceid + "\"";
            ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
            ManagementObjectCollection moCollection = moSearch.Get();
    
            foreach (ManagementObject mo in moCollection)
            {
                if (string.Equals(mo["Antecedent"].ToString(), antecedent))
                    Console.WriteLine("Dependent: " + mo["Dependent"]);
            }
    

    【讨论】:

    • Finnal antecent 变量应该看起来像这样 \\PC-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #0" @Legion 如果我把前面的变量放到 Console.WriteLine (先行);我得到了和我一样的结果,但我相信在 wmi 查询的某个地方它自己是错误的逃逸
    • 好的,所以你的字符串应该是:\\PC-NAME\root\cimv2:Win32_DiskPartition.DeviceID=\"Disk #1, Partition #0\" 因为在另一个“字符串”中(双引号) :) 这就是魔法 --> \" (在标准 C# 字符串中变成 \\\" 因为你也需要 c# 转义码)。
    • 我读了你的评论 10 倍,试过了,它没有逃脱。
    • 我试着解释得更好,你的前行字符串没有错,如果你按原样使用它就没有问题,但是如果你把它作为另一个命令的参数,你需要使用 WMI 转义码,这就是为什么一切看起来都是正确的但是当你使用它时它不起作用:) P.S.:\\PC-NAME\
    • 好的,我会告诉你输出:part.lt/img/43670574ffcd8d5393b0f15ae5ba5b64338.png 第一行是前行,第二行是我想要得到的。如您所见,它们是相同的,除非在 wmi 中一切都不同。如果我将查询代码放在控制台中,我得到:从 Win32_LogicalDiskToPartition 中选择 Dependent,其中 Antecedent = '\\PC-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #0"',我从双 quetes 更改为单,因为我认为 wmi 认为第一个双 quetes 是我正在寻找的参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多