【问题标题】:wmi invalid query errorwmi 无效查询错误
【发布时间】:2012-05-02 05:33:15
【问题描述】:

我正在尝试获取 c:\windows\ntds\ 目录中此 dsa 文件的大小。

我在这里遇到错误,我不知道为什么。错误是“无效查询”。

当我使用不同的 WMI 类时,我似乎经常遇到此错误。

不知道为什么会出现这个错误以及如何解决?

以下代码是否有错误,如何解决?

为什么我们会得到这个 Invalid Query 错误,它的根源是什么?它的内部异常总是为空?

private int getDatabaseFileSize(string DSADatabaseFile, string machineName)
        {
            string scope = @"\\" + machineName + @"\root\CIMV2";
            string query = string.Format("Select FileSize from CIM_DataFile WHERE Name = '{0}'", DSADatabaseFile);
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection collection = searcher.Get();
            foreach (ManagementObject mobj in searcher.Get())
            {
                Console.WriteLine("File Size : " + mobj["FileSize"]);
            }
            return 0;
        }

谢谢

【问题讨论】:

  • 如果DSADatabaseFile 在查询中,你能说出它的值吗?
  • 我没关系,我在管理员帐户下运行这个应用程序。 DSADatabaseFile 的值为 C:\Windows\NTDS\ntds.dit,其大小为 16MB。我遇到了异常,因为它将在 foreach 循环中的关键字中到达。
  • 试试这个C:\\Windows\\NTDS\\ntds.dit 或链接stackoverflow.com/a/972232/1051198
  • 从 Microsoft 下载中心下载 WMI Code Creator:[microsoft.com/en-us/download/…。从 WMI 代码创建器生成要在远程机器上执行的代码,然后尝试。
  • 我很久以前就读过那篇文章,然后才提出问题,并且只使用了相同的内容。但没有运气。

标签: c# .net wmi wql


【解决方案1】:

我猜,但是由于您的查询在语法上是正确的并且使用了正确的字段和对象名称,我认为这是因为您将字符串“C:\Windows\NTDS\ntds.dit”作为DSADatabaseFile 传递。这对于 C# 中的“典型”使用是正确的,例如在使用 Path 类时,等等,但不是在这里。

您需要将带有两个反斜杠的文件名传递给 WMI。但是,由于 C# 已经要求,因此您需要有效地通过四个:

 getDatabaseFileSize("C:\\\\Windows\\\\NTDS\\\\ntds.dit", machine)

或使用逐字字符串文字:

 getDatabaseFileSize(@"C:\\Windows\\NTDS\\ntds.dit", machine);

更新这是一个完整的例子:

// Compile with: csc foo.cs /r:System.Management.dll

using System;
using System.Management;

namespace Foo
{
    public class Program
    {
        private int getDatabaseFileSize(string DSADatabaseFile, string machineName)
        {
            string scope = @"\\" + machineName + @"\root\CIMV2";
            string query = string.Format("Select FileSize from CIM_DataFile WHERE Name = '{0}'", DSADatabaseFile);
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection collection = searcher.Get();
            foreach (ManagementObject mobj in searcher.Get())
            {
                Console.WriteLine("File Size : " + mobj["FileSize"]);
            }
            return 0;
        }

        public static void Main(string[] args)
        {
            var p = new Program();

            // These work
            p.getDatabaseFileSize("C:/boot.ini", ".");
            p.getDatabaseFileSize(@"C:\\boot.ini", ".");
            p.getDatabaseFileSize("C:\\\\boot.ini", ".");

            // These fail
            try {
                p.getDatabaseFileSize("C:\\boot.ini", ".");
            } catch (ManagementException ex) { 
                Console.WriteLine("Failed: {0}", ex.ErrorCode);
            }
            try {
                p.getDatabaseFileSize(@"C:\boot.ini", ".");
            } catch (ManagementException ex) { 
                Console.WriteLine("Failed: {0}", ex.ErrorCode);
            }
        }
    }
}

编译:

(预期的)输出是:

File Size : 313
File Size : 313
Failed: InvalidQuery.
Failed: InvalidQuery.

更新似乎已经有一个related 问题(提到需要\\\\ 而不是\\)。

【讨论】:

  • 不走运,我都试过了。 C:\\Windows\\NTDS\\ntds.dit 和 C:\\\\Windows\\\\NTDS\\\\ntds.dit 也是。同样的错误。
  • 嗯,它在使用您的代码时对我有用(错误和使用双反斜杠的修复)。您可能还想尝试使用单个(正向)斜杠而不是(任何)反斜杠作为目录分隔符。例如。 C:/Windows/ntds/ntds.dit 也适用于我。
  • 我很惊讶它对你有用,但我尝试了各种斜线,它对我不起作用。我经常在使用不同的 wmi 类时遇到此错误。
  • 好吧,“无效查询”,只是意味着传递了一个无效查询。可能是几件事。我通常会在引用未知属性或类,或弄乱语法(关键字之间缺少空格、未闭合引号等)时得到它。
  • 感谢我为不同文件运行测试的信息 getDatabaseFileSize("C:/Windows/bootstat.dat", machineName); getDatabaseFileSize(@"C:\Windows\bootstat.dat", machineName); getDatabaseFileSize(@"C:\\Windows\\bootstat.dat", machineName); getDatabaseFileSize("C:\\\\Windows\\\\bootstat.dat", machineName);同样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多