WMI祥解四:总结

时间:2010年04月12日 点击:239
大家可以看出WMI的功能是很强大的,这里要感谢MicroSoft了,它是永远都不会让我们失望的。WMI对象允许通过VB,VBA,WSH, VBScript, JScript,ASP,或是支持自动对象的其它环境,对WMI进行完全访问。在参考查询体系中加入 WMI Scripting V1.1 Library ,则Visual Basic或是VBA方案就可以访问这些对象了。支持ActiveX程序的操作平台可以通过对象类的代号,或是类的名称创建这些对象,这些对象的前缀是WbemScripting,如 WbemScripting.SwbemLocator。所以大家有兴趣的完全可以利用VB,WSH,VBScript, JScript,ASP等编写更多的利用WMI的黑客程序。

一段有用的程序:
/* **********************************************
* Rainsoft Development Library for Microsoft.NET
*
* Copyright (c) 2004,2005 RainTrail Studio.China
* All Rigths Reserved!
* Author: Q.yuhen (qyuhen@hotmail.com)
**********************************************
*/

using System;
using System.Management;
using System.Collections;
using System.Collections.Specialized;
using System.Text;

namespace Rainsoft.Management
{
WMIPath

/// <summary>
/// 获取系统信息
/// </summary>
/// <example>
/// <code>
/// WMI w = new WMI(WMIPath.Win32_NetworkAdapterConfiguration);
/// for (int i = 0; i < w.Count; i ++)
/// {
/// if ((bool)w[i, "IPEnabled"])
/// {
/// Console.WriteLine("Caption:", w[i, "Caption"]);
/// Console.WriteLine("MAC Address:", w[i, "MACAddress"]);
/// }
/// }
/// </code>
/// </example>

public sealed class WMI
{
private ArrayList mocs;
private StringDictionary names; // 用来存储属性名,便于忽略大小写查询正确名称。

/// <summary>
/// 信息集合数量
/// </summary>

public int Count
{
get { return mocs.Count; }
}


/// <summary>
/// 获取指定属性值,注意某些结果可能是数组。
/// </summary>

public object this[int index, string propertyName]
{
get
{
try
{
string trueName = names[propertyName.Trim()]; // 以此可不区分大小写获得正确的属性名称。
Hashtable h = (Hashtable)mocs[index];
return h[trueName];
}

catch
{
return null;
}

}

}


/// <summary>
/// 返回所有属性名称。
/// </summary>
/// <param name="index"></param>
/// <returns></returns>

public string[] PropertyNames(int index)
{
try
{
Hashtable h
= (Hashtable)mocs[index];
string[] result = new string[h.Keys.Count];

h.Keys.CopyTo(result,
0);

Array.Sort(result);
return result;
}

catch
{
return null;
}

}


/// <summary>
/// 返回测试信息。
/// </summary>
/// <returns></returns>

public string Test()
{
try
{
StringBuilder result
= new StringBuilder(1000);

for (int i = 0; i < Count; i++)
{
int j = 0;
foreach(string s in PropertyNames(i))
{
result.Append(
string.Format(":=\n", ++j, s, this[i, s]));

if (this[i, s] is Array)
{
Array v1
= this[i, s] as Array;
for (int x = 0; x < v1.Length; x++)
{
result.Append(
"\t" + v1.GetValue(x) + "\n");
}

}

}

result.Append(
"======WMI=======\n");
}


return result.ToString();
}

catch
{
return string.Empty;
}

}


/// <summary>
/// 构造函数
/// </summary>
/// <param name="path"></param>

public WMI(string path)
{
names
= new StringDictionary();
mocs
= new ArrayList();

try
{
ManagementClass cimobject
= new ManagementClass(path);
ManagementObjectCollection moc
= cimobject.GetInstances();

bool ok = false;
foreach(ManagementObject mo in moc)
{
Hashtable o
= new Hashtable();
mocs.Add(o);

foreach (PropertyData p in mo.Properties)
{
o.Add(p.Name, p.Value);
if (!ok) names.Add(p.Name, p.Name);
}


ok
= true;
mo.Dispose();
}

moc.Dispose();
}

catch(Exception e)
{
throw new Exception(e.Message);
}

}


/// <summary>
/// 构造函数
/// </summary>
/// <param name="path"></param>

public WMI(WMIPath path): this(path.ToString())
{
}

}

}
更多DotNet好文章www.zdexe.com
更多DotNet好文章www.zdexe.com

赞助商链接

热门内容

相关内容

联系我们

联系方式