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:
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...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With