【发布时间】:2016-07-06 12:17:22
【问题描述】:
尝试执行以下查询时,我不断收到“无效查询”异常:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskQuota WHERE QuotaVolume.DeviceID = 'C:'");
ManagementObjectCollection quotaCollection = searcher.Get();
但是这可行:“SELECT * FROM Win32_DiskQuota”。
根据 MSDN:
对于 WHERE 子句中类描述符的大多数使用,WMI 标记 查询无效并返回错误。但是,请使用点 (.) WMI 中对象类型属性的运算符。例如, 如果 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:
【问题讨论】:
-
看起来不错。您需要捕获异常并将内容粘贴到edit。在对象上调用 ToString,或使用异常对话框(单击底部的“将内容粘贴到剪贴板”链接)。
-
我已经添加了异常但似乎没有太多细节......
-
我用示例代码编辑了我的答案,希望能有所帮助。