c #怎么获取硬件信息

发布时间:2016-12-07 14:57

想知道怎么获取电脑的硬件信息吗,下面是小编带来的关于c #怎么获取硬件信息的内容,欢迎阅读!

c #怎么获取硬件信息?

/// 获取系统信息

///

///

///

/// WMI w = new WMI(WMIPath.Win32_NetworkAdapterConfiguration);

/// for (int i = 0; i < w.Count; i ++)

/// {

/// if ((bool)w[i, "IPEnabled"])

/// {

/// Console.WriteLine("Caption:{0}", w[i, "Caption"]);

/// Console.WriteLine("MAC Address:{0}", w[i, "MACAddress"]);

/// }

/// }

///

///

public sealed class WMI

{

private ArrayList mocs;

private StringDictionary names; // 用来存储属性名,便于忽略大小写查询正确名称。

///

/// 信息集合数量

c #怎么获取硬件信息

public int Count

{

get { return mocs.Count; }

}

///

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

///

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;

}

}

}

///

/// 返回所有属性名称。

///

///

///

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;

}

}

///

/// 返回测试信息。

///

///

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("{0}:{1}={2}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;

}

}

c #怎么获取硬件信息的评论条评论