尝试执行以下查询时,我不断收到"无效查询"异常:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskQuota WHERE QuotaVolume.DeviceID = 'C:'");
ManagementObjectCollection quotaCollection = searcher.Get();
Run Code Online (Sandbox Code Playgroud)
但是这可行:"SELECT*FROM Win32_DiskQuota".
根据MSDN:
对于WHERE子句中类描述符的大多数用法,WMI将查询标记为无效并返回错误.但是,对WMI中的object类型的属性使用点(.)运算符.例如,如果Prop是MyClass的有效属性并且类型为object,则以下查询有效:
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:
Run Code Online (Sandbox Code Playgroud)
是的。根据Win32_DiskQuota 类文档,QuotaVolume 属性是对 Win32_LogicalDisk WMI 类的引用。您提供的 MSDN 报价给出了根据 WQL 规范查询无效的原因。
相反,你可以使用这样的东西:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskQuota WHERE QuotaVolume = \"Win32_LogicalDisk.DeviceID=\\\"C:\\\"\"");
ManagementObjectCollection quotaCollection = searcher.Get();
Run Code Online (Sandbox Code Playgroud)
(注意所有的逃逸......)
| 归档时间: |
|
| 查看次数: |
1131 次 |
| 最近记录: |