關於WMI MSDN有詳細說明。 本文列舉數例算拋磚引玉吧。WMI是個好東西,看過本文後也許你應該能舉一反三參考MSDN也許自己做個優化大師也是可能的。
本文的例子在以下環境調試通過:Windows2003+AMD64雙核CPU+VisualStudio2005(C#)下調試通過,無錯版!
首先要添加“引用”一個dll,選擇“System Management”;
再引入2個命名空間:
using System.Management;
using System.IO;
foreach循環:聲明一個迭代變量自動獲取數組中每個元素的值。
String.Format:格式化字符,本站就有解釋。
代碼:
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//獲取CPU編號
ManagementClass MyClass = new ManagementClass("Win32_Processor");
ManagementObjectCollection MyCollection = MyClass.GetInstances();
String MyInfo = "當前系統CPU編號是:";
string MyCPUID = "";
foreach (ManagementObject MyObject in MyCollection)
{
MyCPUID = MyObject.Properties["ProcessorId"].Value.ToString();
break;
}
MyInfo += MyCPUID;
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button2_Click(object sender, EventArgs e)
{
//獲取計算機CPU的當前電壓
String MyInfo = "計算機CPU的當前電壓是:";
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (ManagementObject MyObject in MySearcher.Get())
{
try
{
MyInfo += "/n" + String.Format("CurrentVoltage : " + MyObject["CurrentVoltage"].ToString());
MyInfo += "/n=========================================================";
}
catch { }
}
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button3_Click(object sender, EventArgs e)
{
//獲取計算機CPU的外部頻率
String MyInfo = "計算機CPU的外部頻率是:";
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (ManagementObject MyObject in MySearcher.Get())
{
try
{
MyInfo += "/n" + String.Format("ExtClock : " + MyObject["ExtClock"].ToString());
MyInfo += "/n=========================================================";
}
catch { }
}
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button4_Click(object sender, EventArgs e)
{
//獲取計算機CPU的二級緩存
String MyInfo = "計算機CPU的二級緩存尺寸是:";
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (ManagementObject MyObject in MySearcher.Get())
{
MyInfo += "/n" + String.Format("L2CacheSize: " + MyObject["L2CacheSize"].ToString());
MyInfo += "/n=========================================================";
}
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button5_Click(object sender, EventArgs e)
{
//獲取計算機CPU的制造商名稱
String MyInfo = "計算機CPU的制造商名稱是:";
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (ManagementObject MyObject in MySearcher.Get())
{
MyInfo += "/n" + String.Format("Manufacturer : " + MyObject["Manufacturer"].ToString());
MyInfo += "/n=========================================================";
}
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button6_Click(object sender, EventArgs e)
{
//獲取計算機CPU的產品名稱
String MyInfo = "計算機CPU的產品名稱是:";
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (ManagementObject MyObject in MySearcher.Get())
{
MyInfo += "/n" + String.Format("Name : " + MyObject["Name"].ToString());
MyInfo += "/n=========================================================";
}
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button7_Click(object sender, EventArgs e)
{
//獲取計算機CPU的版本信息
String MyInfo = "計算機CPU的版本信息如下:";
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (ManagementObject MyObject in MySearcher.Get())
{
MyInfo += "/n" + String.Format("Version: " + MyObject["Version"].ToString());
MyInfo += "/n=========================================================";
}
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button8_Click(object sender, EventArgs e)
{
//獲取計算機CPU的當前使用百分比 注意要把SQLserver或者其他耗CPU的軟件開著否則看不到效果就一直為0
String MyInfo = "計算機CPU的當前使用百分比是:";
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (ManagementObject MyObject in MySearcher.Get())
{
MyInfo += "/n" + String.Format("LoadPercentage : " + MyObject["LoadPercentage"].ToString());
MyInfo += "/n=========================================================";
}
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button9_Click(object sender, EventArgs e)
{
//獲取計算機CPU的最大時鐘頻率
String MyInfo = "計算機CPU的最大時鐘頻率是:";
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (ManagementObject MyObject in MySearcher.Get())
{
MyInfo += "/n" + String.Format("MaxClockSpeed : " + MyObject["MaxClockSpeed"].ToString());
MyInfo += "/n=========================================================";
}
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button10_Click(object sender, EventArgs e)
{
//獲取計算機CPU的當前時鐘頻率
String MyInfo = "計算機CPU的當前時鐘頻率是:";
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (ManagementObject MyObject in MySearcher.Get())
{
MyInfo += "/n" + String.Format("CurrentClockSpeed : " + MyObject["CurrentClockSpeed"].ToString());
MyInfo += "/n=========================================================";
}
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button11_Click(object sender, EventArgs e)
{
//獲取計算機的CPU地址寬度
String MyInfo = "當前計算機的CPU地址寬度是:";
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (ManagementObject MyObject in MySearcher.Get())
{
MyInfo += "/n" + String.Format("AddressWidth: " + MyObject["AddressWidth"].ToString());
MyInfo += "/n=========================================================";
}
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button14_Click(object sender, EventArgs e)
{
//獲取計算機的CPU數據寬度
String MyInfo = "當前計算機的CPU數據寬度是:";
ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (ManagementObject MyObject in MySearcher.Get())
{
MyInfo += "/n" + String.Format("DataWidth : " + MyObject["DataWidth"].ToString());
MyInfo += "/n=========================================================";
}
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}