尝试执行以下查询时,我不断收到“无效查询”异常:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskQuota WHERE QuotaVolume.DeviceID = 'C:'");
ManagementObjectCollection quotaCollection = searcher.Get();
然而这有效:“SELECT * FROM Win32_DiskQuota”。
根据MSDN:
对于WHERE子句中类描述符的大多数用法,WMI标记了 查询无效并返回错误。但是,使用点(。) WMI中类型为object的属性的运算符。例如, 如果Prop是MyClass的有效属性,则以下查询有效 类型对象:
SELECT * FROM MyClass WHERE Prop.embedprop = 5
这是否意味着仅当Prop声明为OBJECT时才有效?
以下是例外情况:
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:
答案 0 :(得分:1)
是。根据{{3}},QuotaVolume属性是对Win32_LogicalDisk WMI类的引用。您提供的MSDN引用了根据WQL规范查询无效的原因。
相反,您可以使用以下内容:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskQuota WHERE QuotaVolume = \"Win32_LogicalDisk.DeviceID=\\\"C:\\\"\"");
ManagementObjectCollection quotaCollection = searcher.Get();
(注意所有逃脱......)