c#怎么獲取硬件信息
想知道怎么獲取電腦的硬件信息嗎,下面是學(xué)習(xí)啦小編帶來的關(guān)于c #怎么獲取硬件信息的內(nèi)容,歡迎閱讀!
c #怎么獲取硬件信息?
/// 獲取系統(tǒng)信息
///
///
///
/// 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; // 用來存儲(chǔ)屬性名,便于忽略大小寫查詢正確名稱。
///
/// 信息集合數(shù)量
public int Count
{
get { return mocs.Count; }
}
///
/// 獲取指定屬性值,注意某些結(jié)果可能是數(shù)組。
///
public object this[int index, string propertyName]
{
get
{
try
{
string trueName = names[propertyName.Trim()]; // 以此可不區(qū)分大小寫獲得正確的屬性名稱。
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;
}
}
///
/// 返回測(cè)試信息。
///
///
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 #怎么獲取硬件信息"文章內(nèi)容的人還看: