【问题标题】:WMI .NET Invalid queryWMI .NET 无效查询
【发布时间】: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,或使用异常对话框(单击底部的“将内容粘贴到剪贴板”链接)。
  • 我已经添加了异常但似乎没有太多细节......
  • 我用示例代码编辑了我的答案,希望能有所帮助。

标签: c# .net wmi wql


【解决方案1】:

是的。根据Win32_DiskQuota class documentation,QuotaVolume 属性是对 Win32_LogicalDisk WMI 类的引用。您提供的 MSDN 报价根据 WQL 规范给出了查询无效的原因。

相反,您可以使用以下内容:

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

(注意所有的转义...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多