如何获取 ManagementObjects 的计数(WMI 结果)而不通过 .NET 中的集合进行枚举
当通过 WMI 查询大量数据时(例如 Windows 事件日志 Win32_NTLogEvent),在下载所有内容之前了解您正在获取的数据类型非常有用。有办法两个人做到这一点吗?
据我所知,WQL 中没有“Select Count(*) FROM Win32_NTLogEvent”。
据我所知,无论将 Rewindable 属性设置为 true 还是 false, ManagementObjectCollection 的 Count 属性实际上都会枚举所有结果。
如果在.NET中无法完成,是否可以直接使用底层IWbem对象来完成 谢谢
When querying for large ammount of data through WMI (say the windows events log Win32_NTLogEvent) it is very useful to know what kind of numbers you are getting yourself into before downloading all the content. Is there a way two do this?
From what i know there is no "Select Count(*) FROM Win32_NTLogEvent" in WQL.
From what i know the Count property of the ManagementObjectCollection actually enumerates through all the results whether you have the Rewindable property set to true or false.
If it cannot be done in .NET, can it be done by directly using the underlying IWbem objects
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
底层 IWbem 对象还返回一个枚举。
例如 IWbemServices::ExecQuery 方法返回 IEnumWbemClassObject
但是,请参阅 < a href="http://msdn.microsoft.com/en-us/library/aa390880(v=VS.85).aspx" rel="nofollow noreferrer">提高枚举性能的一些想法.
值得注意的是,
WBEM_FLAG_FORWARD_ONLY
。如果您使用 C#,我猜它会调用 ManagementObjectSearcher 与 EnumerationOptions.Rewindable 设置为 false。默认情况下,可回卷是正确的,因此关闭它应该会带来一些改进。
(如果您只在查询中请求一个(关键)属性,您还可以分析一下是否有任何性能改进。例如,
Select RecordNumber FROM Win32_NTLogEvent
而不是Select * FROM Win32_NTLogEvent
.理论上不需要实例化那么多信息,尽管实际上它仍然必须枚举所有内容,而且我不记得是否从中看到了任何改进。不过,值得检查一下时间。)
The underlying IWbem objects also return an enumeration.
E.g. The IWbemServices::ExecQuery Method returns an IEnumWbemClassObject
However, see Improving Enumeration Performance for a couple of ideas.
Notably, the
WBEM_FLAG_FORWARD_ONLY
.If you're in C# I'm guessing it would be calling ManagementObjectSearcher with EnumerationOptions.Rewindable set to false. Rewindable is true by default, so turning it off should give some improvement.
(You could also profile to see if there's any performance improvement if you just ask for one (key) property in your query. E.g.
Select RecordNumber FROM Win32_NTLogEvent
instead ofSelect * FROM Win32_NTLogEvent
.In theory not as much info would need to be instantiated, though in realityit still has to enumerate everything, and I don't remember if I ever saw any improvement from that. Worth a timing check, though.)
看来是做不到了。下一个最好的事情是达林上面提供的答案。
It appears that it cannot be done. The next best thing is the answer provided above by Daryn.