5

I keep getting an "Invalid Query" exception when trying to execute the following query:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskQuota WHERE QuotaVolume.DeviceID = 'C:'");
ManagementObjectCollection quotaCollection = searcher.Get();

However this works: "SELECT * FROM Win32_DiskQuota".

According to MSDN:

For most uses of class descriptors in a WHERE clause, WMI flags the query as invalid and returns an error. However, use the dot (.) operator for properties of type object in WMI. For example, the following query is valid if Prop is a valid property of MyClass and is type object:

SELECT * FROM MyClass WHERE Prop.embedprop = 5

Does it mean this only works if Prop declared as OBJECT?

Here are the exception details:

System.Management.ManagementException was unhandled
  HResult=-2146233087
  Message=Invalid query 
  Source=System.Management
  StackTrace:
       в System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
       в System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
       в UserQuota.Program.getQuota() в c:\users\administrator\documents\visual studio 2015\Projects\UserQuota\UserQuota\Program.cs:строка 40
       в UserQuota.Program.Main(String[] args) в c:\users\administrator\documents\visual studio 2015\Projects\UserQuota\UserQuota\Program.cs:строка 33
       в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       в System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       в System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
Vladimir M.
  • 1,049
  • 1
  • 10
  • 24
  • Looks right. You need to catch the exception and paste the contents into an [edit]. Call ToString on the object, or use the exception dialog (click the "paste contents to clipboard" link at the bottom). –  Jul 06 '16 at 12:24
  • 1
    I've added the exception but seems it doesn't have much details... – Vladimir M. Jul 06 '16 at 12:39
  • I edited my answer with sample code which will hopefully help. – Tomer Jul 06 '16 at 13:33

1 Answers1

1

Yes. According to the Win32_DiskQuota class documentation, the QuotaVolume property is a reference to a Win32_LogicalDisk WMI class. The quotation from MSDN you supplied gave the reason why the query is invalid according to the WQL specs.

Instead, you can use something like this:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskQuota WHERE QuotaVolume = \"Win32_LogicalDisk.DeviceID=\\\"C:\\\"\"");
ManagementObjectCollection quotaCollection = searcher.Get();

(Notice all the escaping...)

Tomer
  • 1,606
  • 12
  • 18