对ManagementObjectCollection.Count的引用会引发无效的类异常

时间:2013-03-08 21:46:03

标签: .net wmi

在下面的代码段中,注释行将抛出一个带有“Invalid Class”消息的System.Management.ManagementException。但是,在迭代集合之后引用Count属性可以正常工作。

此代码来自正在运行的单元测试,然后开始失败。我无法确定改变了什么。

ManagementScope scope = new ManagementScope(@"root\mypath");
scope.Options.Impersonation = ImpersonationLevel.Impersonate;
ObjectQuery query = new ObjectQuery("SELECT * FROM myclass");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection result = searcher.Get();
//int count = result.Count // throws exception
foreach (ManagementObject mo in result)
{
    Console.WriteLine(mo.Path);
}
int count = result.Count // no problem

任何想法?

0 个答案:

没有答案