我有一些代码可以从C#中的WMI中获取一些信息,用于连接到计算机的显示器。代码是这样的......
String queryString = "Select * from Win32_DesktopMonitor";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(queryString)
foreach (ManagementObject obj in searcher.Get())
{
...
}
如何在枚举期间计算从此查询返回的对象数量?
对此非常新,所以要温柔! : - )
答案 0 :(得分:4)
我认为你必须运行Get()
函数才能获得对象的数量:
String queryString = "Select * from Win32_DesktopMonitor";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(queryString);
MessageBox.Show("Found: " + searcher.Get().Count.ToString());
答案 1 :(得分:1)
我想你不要问:
String queryString = "Select * from Win32_DesktopMonitor";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(queryString);
Int32 i = 0;
foreach (ManagementObject obj in searcher.Get())
{
i++;
}
// I have one computer so, i = 1
答案 2 :(得分:0)
这里的最佳方法是Count in Classes ManagementObjectColletion或ManagementObjectSearcher。如果你要使用结果,那么它的优点是:
Array ( [0] => 248, 242 )
并对大小进行验证:
ManagementObjectCollection jobs = searchJob.Get();