Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# wmi无效查询错误_C#_.net_Wmi_Wql - Fatal编程技术网

C# wmi无效查询错误

C# wmi无效查询错误,c#,.net,wmi,wql,C#,.net,Wmi,Wql,我正在尝试获取c:\windows\ntds\目录中此dsa文件的大小 我在这里出错了,我不知道为什么。错误为“无效查询” 当我使用不同的WMI类时,我似乎经常遇到这个错误 我不知道为什么会发生此错误以及如何解决此问题? private int getDatabaseFileSize(string DSADatabaseFile, string machineName) { string scope = @"\\" + machineName + @"\r

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

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

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

我不知道为什么会发生此错误以及如何解决此问题?

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;
        }
下面的代码中是否有错误,如何解决?

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;
        }
为什么会出现这个无效的查询错误,它的来源是什么?其内部异常将始终为空?

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

谢谢

我猜,但是由于您的查询在语法上是正确的,并且使用了正确的字段和对象名称,我假设这是因为您将字符串“C:\Windows\NTDS\NTDS.dit”作为
DSADatabaseFile
传递。这对于C#内部的“典型”用法是正确的,比如当使用
路径
类时,或者诸如此类的时候,但不是在这里

您需要将带有两个反斜杠的文件名传递给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.

更新似乎已经有问题了(提到需要使用
\\\\\\\\\
而不是
\\\\
)。

你能告诉我查询中
DSADatabaseFile
的值是多少吗?我知道了,我正在管理员帐户下运行这个应用程序。DSADatabaseFile的值为C:\Windows\NTDS\NTDS.dit,其大小为16MB。我收到异常,因为它将在foreach循环中的关键字中到达。请尝试此
C:\\Windows\\NTDS\\NTDS.dit
或从Microsoft下载中心的下载WMI代码创建者链接:[.从WMI代码创建者生成要在远程计算机上执行的代码,然后重试。我很久以前在提出问题之前阅读过这篇文章,只使用了相同的内容。但是没有运气。没有运气,我尝试了所有内容。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);相同错误。