同样的,先秀一下效果图。要获取的信息包括CPU名称,CPU序列号,CPU个数,CPU制造商,CPU当前时钟频率,CPU最大时钟频率,CPU外部频率,CPU当前电压,CPU二级缓存,CPU数据带宽,CPU地址带宽,CPU使用率。
同样的,需要一个类库,取名为:CPUInfo,类图如下:
CPUInfo
public class CPUInfo
{
属性#region 属性
CPU名称#region CPU名称
string strCPUName = string.Empty;
public string CPUName
{
get { return strCPUName; }
set { strCPUName = value; }
}
#endregion
CPU序列号#region CPU序列号
string strCPUID = string.Empty;
public string CPUID
{
get { return strCPUID; }
set { strCPUID = value; }
}
#endregion
CPU个数#region CPU个数
int nCPUCount = 0;
public int CPUCount
{
get { return nCPUCount; }
set { nCPUCount = value; }
}
#endregion
CPU制造商#region CPU制造商
string strCPUManufacturer = string.Empty;
public string CPUManufacturer
{
get { return strCPUManufacturer; }
set { strCPUManufacturer = value; }
}
#endregion
当前时钟频率#region 当前时钟频率
string strCPUCurrentClockSpeed = string.Empty;
public string CPUCurrentClockSpeed
{
get { return strCPUCurrentClockSpeed; }
set { strCPUCurrentClockSpeed = value; }
}
#endregion
最大时钟频率#region 最大时钟频率
string strCPUMaxClockSpeed = string.Empty;
public string CPUMaxClockSpeed
{
get { return strCPUMaxClockSpeed; }
set { strCPUMaxClockSpeed = value; }
}
#endregion
外部频率#region 外部频率
string strCPUExtClock = string.Empty;
public string CPUExtClock
{
get { return strCPUExtClock; }
set { strCPUExtClock = value; }
}
#endregion
当前电压#region 当前电压
string strCPUCurrentVoltage = string.Empty;
public string CPUCurrentVoltage
{
get { return strCPUCurrentVoltage; }
set { strCPUCurrentVoltage = value; }
}
#endregion
二级缓存#region 二级缓存
string strCPUL2CacheSize = string.Empty;
public string CPUL2CacheSize
{
get { return strCPUL2CacheSize; }
set { strCPUL2CacheSize = value; }
}
#endregion
数据带宽#region 数据带宽
string strCPUDataWidth = string.Empty;
public string CPUDataWidth
{
get { return strCPUDataWidth; }
set { strCPUDataWidth = value; }
}
#endregion
地址带宽#region 地址带宽
string strCPUAddressWidth = string.Empty;
public string CPUAddressWidth
{
get { return strCPUAddressWidth; }
set { strCPUAddressWidth = value; }
}
#endregion
使用百分比#region 使用百分比
float fCPUUsedPercent;
public float CPUUsedPercent
{
get { return fCPUUsedPercent; }
set { fCPUUsedPercent = value; }
}
#endregion
CPU温度#region CPU温度
double strCPUTemperature;
public double CPUTemperature
{
get { return strCPUTemperature; }
set { strCPUTemperature = value; }
}
#endregion
#endregion
构造函数#region 构造函数
public CPUInfo()
{
//GetCPUInfo();
}
#endregion
GetCPUInfo#region GetCPUInfo
public void GetCPUInfo()
{
使用百分比#region 使用百分比
GetCPULoadPercentage();
#endregion
CPUCount = Environment.ProcessorCount; //CPU个数
GetCPUCurrentTemperature();
//实例化一个ManagementClass类,并将Win32_Processor作为参数传递进去,
//这样就可以查询Win32_Processor这个类里面的一些信息了。
ManagementClass mClass = new ManagementClass("Win32_Processor");
//获取Win32_Processor这个类的所有实例
ManagementObjectCollection moCollection = mClass.GetInstances();
//对Win32_Processor这个类进行遍历
foreach (ManagementObject mObject in moCollection)
{
CPUName = mObject["Name"].ToString(); //获取CPU名称
CPUID = mObject["ProcessorId"].ToString(); //获取 CPU ID
CPUManufacturer = mObject["Manufacturer"].ToString(); //获取CPU制造商
CPUCurrentClockSpeed = mObject["CurrentClockSpeed"].ToString(); //获取当前时钟频率
CPUMaxClockSpeed = mObject["MaxClockSpeed"].ToString(); //获取最大时钟频率
CPUExtClock = mObject["ExtClock"].ToString(); //获取外部频率
CPUCurrentVoltage = mObject["CurrentVoltage"].ToString(); //获取当前电压
CPUL2CacheSize = mObject["L2CacheSize"].ToString(); //获取二级缓存
CPUDataWidth = mObject["DataWidth"].ToString(); //获取数据带宽
CPUAddressWidth = mObject["AddressWidth"].ToString(); //获取地址带宽
}
}
public void GetCPUCurrentTemperature()
{
ManagementObjectSearcher mos = new ManagementObjectSearcher(@"root\wmi", @"select * from MSAcpi_ThermalZoneTemperature");
foreach (ManagementObject mo in mos.Get())
{
CPUTemperature = Convert.ToDouble(Convert.ToDouble(mo["CurrentTemperature"].ToString()) - 2732) / 10;
}
}
public void GetCPULoadPercentage()
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_Processor");
foreach (ManagementObject queryObj in searcher.Get())
{
CPUUsedPercent = float.Parse(queryObj["LoadPercentage"].ToString());
}
}
#endregion
}
public class CPUInfo
{
属性#region 属性
CPU名称#region CPU名称
string strCPUName = string.Empty;
public string CPUName
{
get { return strCPUName; }
set { strCPUName = value; }
}
#endregion
CPU序列号#region CPU序列号
string strCPUID = string.Empty;
public string CPUID
{
get { return strCPUID; }
set { strCPUID = value; }
}
#endregion
CPU个数#region CPU个数
int nCPUCount = 0;
public int CPUCount
{
get { return nCPUCount; }
set { nCPUCount = value; }
}
#endregion
CPU制造商#region CPU制造商
string strCPUManufacturer = string.Empty;
public string CPUManufacturer
{
get { return strCPUManufacturer; }
set { strCPUManufacturer = value; }
}
#endregion
当前时钟频率#region 当前时钟频率
string strCPUCurrentClockSpeed = string.Empty;
public string CPUCurrentClockSpeed
{
get { return strCPUCurrentClockSpeed; }
set { strCPUCurrentClockSpeed = value; }
}
#endregion
最大时钟频率#region 最大时钟频率
string strCPUMaxClockSpeed = string.Empty;
public string CPUMaxClockSpeed
{
get { return strCPUMaxClockSpeed; }
set { strCPUMaxClockSpeed = value; }
}
#endregion
外部频率#region 外部频率
string strCPUExtClock = string.Empty;
public string CPUExtClock
{
get { return strCPUExtClock; }
set { strCPUExtClock = value; }
}
#endregion
当前电压#region 当前电压
string strCPUCurrentVoltage = string.Empty;
public string CPUCurrentVoltage
{
get { return strCPUCurrentVoltage; }
set { strCPUCurrentVoltage = value; }
}
#endregion
二级缓存#region 二级缓存
string strCPUL2CacheSize = string.Empty;
public string CPUL2CacheSize
{
get { return strCPUL2CacheSize; }
set { strCPUL2CacheSize = value; }
}
#endregion
数据带宽#region 数据带宽
string strCPUDataWidth = string.Empty;
public string CPUDataWidth
{
get { return strCPUDataWidth; }
set { strCPUDataWidth = value; }
}
#endregion
地址带宽#region 地址带宽
string strCPUAddressWidth = string.Empty;
public string CPUAddressWidth
{
get { return strCPUAddressWidth; }
set { strCPUAddressWidth = value; }
}
#endregion
使用百分比#region 使用百分比
float fCPUUsedPercent;
public float CPUUsedPercent
{
get { return fCPUUsedPercent; }
set { fCPUUsedPercent = value; }
}
#endregion
CPU温度#region CPU温度
double strCPUTemperature;
public double CPUTemperature
{
get { return strCPUTemperature; }
set { strCPUTemperature = value; }
}
#endregion
#endregion
构造函数#region 构造函数
public CPUInfo()
{
//GetCPUInfo();
}
#endregion
GetCPUInfo#region GetCPUInfo
public void GetCPUInfo()
{
使用百分比#region 使用百分比
GetCPULoadPercentage();
#endregion
CPUCount = Environment.ProcessorCount; //CPU个数
GetCPUCurrentTemperature();
//实例化一个ManagementClass类,并将Win32_Processor作为参数传递进去,
//这样就可以查询Win32_Processor这个类里面的一些信息了。
ManagementClass mClass = new ManagementClass("Win32_Processor");
//获取Win32_Processor这个类的所有实例
ManagementObjectCollection moCollection = mClass.GetInstances();
//对Win32_Processor这个类进行遍历
foreach (ManagementObject mObject in moCollection)
{
CPUName = mObject["Name"].ToString(); //获取CPU名称
CPUID = mObject["ProcessorId"].ToString(); //获取 CPU ID
CPUManufacturer = mObject["Manufacturer"].ToString(); //获取CPU制造商
CPUCurrentClockSpeed = mObject["CurrentClockSpeed"].ToString(); //获取当前时钟频率
CPUMaxClockSpeed = mObject["MaxClockSpeed"].ToString(); //获取最大时钟频率
CPUExtClock = mObject["ExtClock"].ToString(); //获取外部频率
CPUCurrentVoltage = mObject["CurrentVoltage"].ToString(); //获取当前电压
CPUL2CacheSize = mObject["L2CacheSize"].ToString(); //获取二级缓存
CPUDataWidth = mObject["DataWidth"].ToString(); //获取数据带宽
CPUAddressWidth = mObject["AddressWidth"].ToString(); //获取地址带宽
}
}
public void GetCPUCurrentTemperature()
{
ManagementObjectSearcher mos = new ManagementObjectSearcher(@"root\wmi", @"select * from MSAcpi_ThermalZoneTemperature");
foreach (ManagementObject mo in mos.Get())
{
CPUTemperature = Convert.ToDouble(Convert.ToDouble(mo["CurrentTemperature"].ToString()) - 2732) / 10;
}
}
public void GetCPULoadPercentage()
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_Processor");
foreach (ManagementObject queryObj in searcher.Get())
{
CPUUsedPercent = float.Parse(queryObj["LoadPercentage"].ToString());
}
}
#endregion
}
然后在HostPerformance添加一个窗体,命名为:CPUInfoForm,代码如下:
CPUInfo Form
public partial class CPUInfoForm : Form
{
public CPUInfoForm()
{
InitializeComponent();
}
ComputerInfo.CPUInfo cpuInfo = new ComputerInfo.CPUInfo();
private void timer1_Tick(object sender, EventArgs e)
{
lvCPU.Items[lvCPU.Items.Count - 1].SubItems[1].Text = cpuInfo.CPUUsedPercent.ToString();
lvCPU.Items[lvCPU.Items.Count - 2].SubItems[1].Text = cpuInfo.CPUTemperature.ToString();
}
public void DrawLineChart()
{
while (true)
{
cpuInfo.GetCPULoadPercentage();
float f = cpuInfo.CPUUsedPercent;
lineChart1.Add(f / 100F);
}
}
Thread oThread;
private void CPUInfoForm_Load(object sender, EventArgs e)
{
#region 设置ListView的样式以及列
lvCPU.View = View.Details;
lvCPU.Columns.Add("项", 150);
lvCPU.Columns.Add("值", 300);
lvCPU.FullRowSelect = true;
lvCPU.GridLines = true;
#endregion
cpuInfo.GetCPUInfo();
#region 定义一些需要用到的项
ListViewItem lviCPUName = new ListViewItem(new string[] { "CPU 名称", cpuInfo.CPUName });
ListViewItem lviCPUID = new ListViewItem(new string[] { "CPU 序列号", cpuInfo.CPUID });
ListViewItem lviCPUCount = new ListViewItem(new string[] { "CPU 个数", cpuInfo.CPUCount.ToString() });
ListViewItem lviCPUManufacturer = new ListViewItem(new string[] { "CPU 制造商", cpuInfo.CPUManufacturer });
ListViewItem lviCPUCurrentClockSpeed = new ListViewItem(new string[] { "CPU 当前时钟频率", cpuInfo.CPUCurrentClockSpeed });
ListViewItem lviCPUMaxClockSpeed = new ListViewItem(new string[] { "CPU 最大时钟频率", cpuInfo.CPUMaxClockSpeed });
ListViewItem lviCPUExtClock = new ListViewItem(new string[] { "CPU 外部频率", cpuInfo.CPUExtClock });
ListViewItem lviCPUCurrentVoltage = new ListViewItem(new string[] { "CPU 当前电压", cpuInfo.CPUCurrentVoltage });
ListViewItem lviCPUL2CacheSize = new ListViewItem(new string[] { "CPU 二级缓存", cpuInfo.CPUL2CacheSize });
ListViewItem lviCPUDataWidth = new ListViewItem(new string[] { "CPU 数据带宽", cpuInfo.CPUDataWidth });
ListViewItem lviCPUAddressWidth = new ListViewItem(new string[] { "CPU 地址带宽", cpuInfo.CPUAddressWidth });
ListViewItem lviCPUTemperature = new ListViewItem(new string[] { "CPU 温度", cpuInfo.CPUTemperature.ToString() });
ListViewItem lviCPUUsedPercent = new ListViewItem(new string[] { "CPU 使用率", cpuInfo.CPUUsedPercent.ToString() });
#endregion
#region 将项添加到ListView控件里面
lvCPU.Items.Add(lviCPUName);
lvCPU.Items.Add(lviCPUID);
lvCPU.Items.Add(lviCPUCount);
lvCPU.Items.Add(lviCPUManufacturer);
lvCPU.Items.Add(lviCPUCurrentClockSpeed);
lvCPU.Items.Add(lviCPUMaxClockSpeed);
lvCPU.Items.Add(lviCPUExtClock);
lvCPU.Items.Add(lviCPUCurrentVoltage);
lvCPU.Items.Add(lviCPUL2CacheSize);
lvCPU.Items.Add(lviCPUDataWidth);
lvCPU.Items.Add(lviCPUAddressWidth);
//lvCPU.Items.Add(lviCPUTemperature);
lvCPU.Items.Add(lviCPUUsedPercent);
#endregion
timerCPULine.Enabled = true;
#region 新开一个线程进行绘图
oThread = new Thread(new ThreadStart(DrawLineChart));
oThread.Start();
#endregion
}
private void CPUInfoForm_FormClosing(object sender, FormClosingEventArgs e)
{
oThread.Abort();
}
}
public partial class CPUInfoForm : Form
{
public CPUInfoForm()
{
InitializeComponent();
}
ComputerInfo.CPUInfo cpuInfo = new ComputerInfo.CPUInfo();
private void timer1_Tick(object sender, EventArgs e)
{
lvCPU.Items[lvCPU.Items.Count - 1].SubItems[1].Text = cpuInfo.CPUUsedPercent.ToString();
lvCPU.Items[lvCPU.Items.Count - 2].SubItems[1].Text = cpuInfo.CPUTemperature.ToString();
}
public void DrawLineChart()
{
while (true)
{
cpuInfo.GetCPULoadPercentage();
float f = cpuInfo.CPUUsedPercent;
lineChart1.Add(f / 100F);
}
}
Thread oThread;
private void CPUInfoForm_Load(object sender, EventArgs e)
{
#region 设置ListView的样式以及列
lvCPU.View = View.Details;
lvCPU.Columns.Add("项", 150);
lvCPU.Columns.Add("值", 300);
lvCPU.FullRowSelect = true;
lvCPU.GridLines = true;
#endregion
cpuInfo.GetCPUInfo();
#region 定义一些需要用到的项
ListViewItem lviCPUName = new ListViewItem(new string[] { "CPU 名称", cpuInfo.CPUName });
ListViewItem lviCPUID = new ListViewItem(new string[] { "CPU 序列号", cpuInfo.CPUID });
ListViewItem lviCPUCount = new ListViewItem(new string[] { "CPU 个数", cpuInfo.CPUCount.ToString() });
ListViewItem lviCPUManufacturer = new ListViewItem(new string[] { "CPU 制造商", cpuInfo.CPUManufacturer });
ListViewItem lviCPUCurrentClockSpeed = new ListViewItem(new string[] { "CPU 当前时钟频率", cpuInfo.CPUCurrentClockSpeed });
ListViewItem lviCPUMaxClockSpeed = new ListViewItem(new string[] { "CPU 最大时钟频率", cpuInfo.CPUMaxClockSpeed });
ListViewItem lviCPUExtClock = new ListViewItem(new string[] { "CPU 外部频率", cpuInfo.CPUExtClock });
ListViewItem lviCPUCurrentVoltage = new ListViewItem(new string[] { "CPU 当前电压", cpuInfo.CPUCurrentVoltage });
ListViewItem lviCPUL2CacheSize = new ListViewItem(new string[] { "CPU 二级缓存", cpuInfo.CPUL2CacheSize });
ListViewItem lviCPUDataWidth = new ListViewItem(new string[] { "CPU 数据带宽", cpuInfo.CPUDataWidth });
ListViewItem lviCPUAddressWidth = new ListViewItem(new string[] { "CPU 地址带宽", cpuInfo.CPUAddressWidth });
ListViewItem lviCPUTemperature = new ListViewItem(new string[] { "CPU 温度", cpuInfo.CPUTemperature.ToString() });
ListViewItem lviCPUUsedPercent = new ListViewItem(new string[] { "CPU 使用率", cpuInfo.CPUUsedPercent.ToString() });
#endregion
#region 将项添加到ListView控件里面
lvCPU.Items.Add(lviCPUName);
lvCPU.Items.Add(lviCPUID);
lvCPU.Items.Add(lviCPUCount);
lvCPU.Items.Add(lviCPUManufacturer);
lvCPU.Items.Add(lviCPUCurrentClockSpeed);
lvCPU.Items.Add(lviCPUMaxClockSpeed);
lvCPU.Items.Add(lviCPUExtClock);
lvCPU.Items.Add(lviCPUCurrentVoltage);
lvCPU.Items.Add(lviCPUL2CacheSize);
lvCPU.Items.Add(lviCPUDataWidth);
lvCPU.Items.Add(lviCPUAddressWidth);
//lvCPU.Items.Add(lviCPUTemperature);
lvCPU.Items.Add(lviCPUUsedPercent);
#endregion
timerCPULine.Enabled = true;
#region 新开一个线程进行绘图
oThread = new Thread(new ThreadStart(DrawLineChart));
oThread.Start();
#endregion
}
private void CPUInfoForm_FormClosing(object sender, FormClosingEventArgs e)
{
oThread.Abort();
}
}
上面用于显示波形图的控件是从网上找的,这个控件的作者是园里lemony朋友的《用C#开发较完整的Windows任务管理器》里面抠出来的
下面附上这个控件的源代码。/Files/lxcsmallcity/HostPerformance/LineChart.rar
至此,CPU信息的小功能就完成了,不过,有一个问题,在获取CPU温度的时候遇到一些问题,我的做法是GetCPUCurrentTemperature()这个方法,但是获取的数据时错误的,而且上网找了很久,也还没找到如何获取CPU温度,不知道哪位大哥知道用C#语言获取CPU温度的,麻烦告诉我下,谢谢
未完,待续~~~