制作了一个WMI 查询分析工具

按微软的介绍大致如下:
WMI 是 Microsoft 主要的针对 Windows 的管理支持技术。在 WMI 之前,所有的 Windows 图形化管理工具都依赖于 Win32 应用程序编程接口(Application Programming Interfaces,APIs)来访问和管理 Windows 资源。在 WMI 之前,能够以编程方式访问 Windows 资源的惟一方法就是通过 Win32 API。这种情况使 Windows 系统管理员无法通过一种简便的方法利用常见的脚本语言来自动化常用的系统管理任务,因为大多数脚本语言都不能直接调用 Win32 API。通过提供一致的模型和框架,WMI 改变了这种情况 — 通过模型和框架,所有的 Windows 资源均被描述并公开给外界。最好的一点是,系统管理员可以使用 WMI 脚本库创建系统管理脚本,从而管理任何通过 WMI 公开的 Windows 资源!

我已经不记得什么时候就知道的这个WMI,但直到今天才认真的学学;可能是采用SQL的语法吸引了我吧,至少用它来做到“windows 优化大师”所获取的系统信息是很容易的。晚上一直在弄这个小程序,因为WMI设计到windows的方方面面,所以我想做一个想sqlserver带的查询分析器的工具,专门用于调试WMI的查询;用了后才发现使用起来确实方便,.net提供一个System.Management.ManagementObjectSearcher类,用它很简单的就可操作WMI;如下是简单实例:

None.gif ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
None.gif sysInfoListTest.Items.Clear();
None.gif sysInfoListTest.Items.Add("Computer System Information");
None.gif foreach(ManagementObject info in search.Get())
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
InBlock.gif sysInfoListTest.Items.Add("Manufacturer: " + info["manufacturer"].ToString());
InBlock.gif sysInfoListTest.Items.Add("Model: " + info["model"].ToString());
InBlock.gif sysInfoListTest.Items.Add("System Type: " + info["systemtype"].ToString());
InBlock.gif sysInfoListTest.Items.Add("Total Physical Memory: " + info["totalphysicalmemory"].ToString());
ExpandedBlockEnd.gif }

这个是访问计算机系统的一些基础信息,比如可以知道系统的“物理内存”大小;

我在程序里实现了一个内存表,专门用于dataGrid来显示数据,通过访问 ManagementObject的属性信息,把每个类的名称作为列显示出来;下面的代码就是实现的核心:
None.gif foreach(ManagementObject mo in mos.Get())
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
InBlock.gif DataRow dr = dt.NewRow();
InBlock.gif
InBlock.gif PropertyDataCollection.PropertyDataEnumerator oEnum;
InBlock.gif//显示系统类
InBlock.gif
//获取属性,系统类
InBlock.gif
oEnum = (mo.Properties.GetEnumerator() as PropertyDataCollection.PropertyDataEnumerator);
InBlock.gifwhile(oEnum.MoveNext())
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif PropertyData prop = (PropertyData)oEnum.Current;
InBlock.gifif(dt.Columns.IndexOf(prop.Name)==-1)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif dt.Columns.Add(prop.Name);
InBlock.gif Console.Out.WriteLine(prop.Name);
InBlock.gif dt.Columns[dt.Columns.Count-1].DefaultValue = "";
ExpandedSubBlockEnd.gif }
InBlock.giftry
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif//该类未实例化,所以需要异常结构,表示为null
InBlock.gif
dr[prop.Name] = mo[prop.Name].ToString();
InBlock.gif Console.Out.WriteLine(prop.Name + " row");
ExpandedSubBlockEnd.gif }
InBlock.gifcatch
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif Console.Out.WriteLine(prop.Name + " row error");
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedBlockEnd.gif }


可以看到这句代码 :
mo.Properties.GetEnumerator() as PropertyDataCollection.PropertyDataEnumerator
这里的Properties所访问的是非系统属性集合,既然是调试工具那么就得把SystemProperties也列出来,实现的过程同上。

下面的帖图是运行界面:

WQLface.png
PS:我刻意缩小了,因为每次帖的图片过大了


下次再实现读出所有的可访问的类;




本文转自suifei博客园博客,原文链接:http://www.cnblogs.com/Chinasf/archive/2005/05/26/162545.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值