同样的,先秀一下效果图。要获取的信息包括CPU名称,CPU序列号,CPU个数,CPU制造商,CPU当前时钟频率,CPU最大时钟频率,CPU外部频率,CPU当前电压,CPU二级缓存,CPU数据带宽,CPU地址带宽,CPU使用率。
![](https://images.cnblogs.com/cnblogs_com/lxcsmallcity/HostPerformance/2/1.png)
同样的,需要一个类库,取名为:CPUInfo,类图如下:
![](https://images.cnblogs.com/cnblogs_com/lxcsmallcity/HostPerformance/2/2.png)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
CPUInfo
public class CPUInfo
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
属性#region 属性
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
CPU名称#region CPU名称
string strCPUName = string.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string CPUName
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return strCPUName; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ strCPUName = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
CPU序列号#region CPU序列号
string strCPUID = string.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string CPUID
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return strCPUID; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ strCPUID = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
CPU个数#region CPU个数
int nCPUCount = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int CPUCount
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return nCPUCount; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ nCPUCount = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
CPU制造商#region CPU制造商
string strCPUManufacturer = string.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string CPUManufacturer
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return strCPUManufacturer; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ strCPUManufacturer = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
当前时钟频率#region 当前时钟频率
string strCPUCurrentClockSpeed = string.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string CPUCurrentClockSpeed
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return strCPUCurrentClockSpeed; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ strCPUCurrentClockSpeed = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
最大时钟频率#region 最大时钟频率
string strCPUMaxClockSpeed = string.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string CPUMaxClockSpeed
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return strCPUMaxClockSpeed; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ strCPUMaxClockSpeed = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
外部频率#region 外部频率
string strCPUExtClock = string.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string CPUExtClock
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return strCPUExtClock; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ strCPUExtClock = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
当前电压#region 当前电压
string strCPUCurrentVoltage = string.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string CPUCurrentVoltage
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return strCPUCurrentVoltage; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ strCPUCurrentVoltage = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
二级缓存#region 二级缓存
string strCPUL2CacheSize = string.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string CPUL2CacheSize
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return strCPUL2CacheSize; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ strCPUL2CacheSize = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
数据带宽#region 数据带宽
string strCPUDataWidth = string.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string CPUDataWidth
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return strCPUDataWidth; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ strCPUDataWidth = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
地址带宽#region 地址带宽
string strCPUAddressWidth = string.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string CPUAddressWidth
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return strCPUAddressWidth; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ strCPUAddressWidth = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
使用百分比#region 使用百分比
float fCPUUsedPercent;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public float CPUUsedPercent
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return fCPUUsedPercent; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ fCPUUsedPercent = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
CPU温度#region CPU温度
double strCPUTemperature;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public double CPUTemperature
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return strCPUTemperature; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ strCPUTemperature = value; }
}
#endregion
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
构造函数#region 构造函数
public CPUInfo()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//GetCPUInfo();
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
GetCPUInfo#region GetCPUInfo
public void GetCPUInfo()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
使用百分比#region 使用百分比
GetCPULoadPercentage();
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CPUCount = Environment.ProcessorCount; //CPU个数
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
GetCPUCurrentTemperature();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//实例化一个ManagementClass类,并将Win32_Processor作为参数传递进去,
//这样就可以查询Win32_Processor这个类里面的一些信息了。
ManagementClass mClass = new ManagementClass("Win32_Processor");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//获取Win32_Processor这个类的所有实例
ManagementObjectCollection moCollection = mClass.GetInstances();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//对Win32_Processor这个类进行遍历
foreach (ManagementObject mObject in moCollection)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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(); //获取地址带宽
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void GetCPUCurrentTemperature()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher(@"root\wmi", @"select * from MSAcpi_ThermalZoneTemperature");
foreach (ManagementObject mo in mos.Get())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CPUTemperature = Convert.ToDouble(Convert.ToDouble(mo["CurrentTemperature"].ToString()) - 2732) / 10;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void GetCPULoadPercentage()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_Processor");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (ManagementObject queryObj in searcher.Get())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CPUUsedPercent = float.Parse(queryObj["LoadPercentage"].ToString());
}
}
#endregion
}
然后在HostPerformance添加一个窗体,命名为:CPUInfoForm,代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
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();
}
}
上面用于显示波形图的控件是从网上找的,这个控件的作者是园里lemony朋友的《用C#开发较完整的Windows任务管理器》里面抠出来的
下面附上这个控件的源代码。/Files/lxcsmallcity/HostPerformance/LineChart.rar
至此,CPU信息的小功能就完成了,不过,有一个问题,在获取CPU温度的时候遇到一些问题,我的做法是GetCPUCurrentTemperature()这个方法,但是获取的数据时错误的,而且上网找了很久,也还没找到如何获取CPU温度,不知道哪位大哥知道用C#语言获取CPU温度的,麻烦告诉我下,谢谢![](https://www.cnblogs.com/Emoticons/qface/055243929.gif)
未完,待续~~~