Question

I am trying to get the size of this dsa file present in c:\windows\ntds\ directory.

I am getting error here, and I don’t know why. The error is “Invalid Query”.

I seems to get this error quite often when I work with different WMI classes.

I don’t know why this error occur and how to resolve this?

Is there any error in below code, How to resolve this?

Why we will get this Invalid Query error, whats the source of it? Its inner exception will always be null?

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

Thanks

Was it helpful?

Solution

I'm guessing, but since your query is syntactically correct and uses the correct field and object names, I assume it is because you pass the string "C:\Windows\NTDS\ntds.dit" as DSADatabaseFile. That would by correct for "typical" use inside C#, like when using the Path class, or such, but not here.

You need to pass the filename with two backslashes to WMI. Since, however, C# requires to already, you need to effectively pass four:

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

or using a verbatim string literal:

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

Update Here is a complete example:

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

Compile with:

The (expected) output is:

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

Update There seems to be a related question already (mentioning the need for \\\\ instead of \\).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top