主机性能监控系统--2.获取CPU性能信息

      同样的,先秀一下效果图。要获取的信息包括CPU名称,CPU序列号,CPU个数,CPU制造商,CPU当前时钟频率,CPU最大时钟频率,CPU外部频率,CPU当前电压,CPU二级缓存,CPU数据带宽,CPU地址带宽,CPU使用率。

同样的,需要一个类库,取名为:CPUInfo,类图如下:

 

ContractedBlock.gif ExpandedBlockStart.gif CPUInfo
    public class CPUInfo
ExpandedBlockStart.gifContractedBlock.gif    
{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
属性#region 属性
ContractedSubBlock.gifExpandedSubBlockStart.gif        
CPU名称#region CPU名称
        
string strCPUName = string.Empty;

        
public string CPUName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strCPUName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strCPUName = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
CPU序列号#region CPU序列号
        
string strCPUID = string.Empty;

        
public string CPUID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strCPUID; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strCPUID = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
CPU个数#region CPU个数
        
int nCPUCount = 0;

        
public int CPUCount
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return nCPUCount; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { nCPUCount = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
CPU制造商#region CPU制造商
        
string strCPUManufacturer = string.Empty;

        
public string CPUManufacturer
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strCPUManufacturer; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strCPUManufacturer = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
当前时钟频率#region 当前时钟频率
        
string strCPUCurrentClockSpeed = string.Empty;

        
public string CPUCurrentClockSpeed
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strCPUCurrentClockSpeed; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strCPUCurrentClockSpeed = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
最大时钟频率#region 最大时钟频率
        
string strCPUMaxClockSpeed = string.Empty;

        
public string CPUMaxClockSpeed
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strCPUMaxClockSpeed; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strCPUMaxClockSpeed = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
外部频率#region 外部频率
        
string strCPUExtClock = string.Empty;

        
public string CPUExtClock
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strCPUExtClock; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strCPUExtClock = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
当前电压#region 当前电压
        
string strCPUCurrentVoltage = string.Empty;

        
public string CPUCurrentVoltage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strCPUCurrentVoltage; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strCPUCurrentVoltage = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
二级缓存#region 二级缓存
        
string strCPUL2CacheSize = string.Empty;

        
public string CPUL2CacheSize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strCPUL2CacheSize; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strCPUL2CacheSize = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
数据带宽#region 数据带宽
        
string strCPUDataWidth = string.Empty;

        
public string CPUDataWidth
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strCPUDataWidth; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strCPUDataWidth = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
地址带宽#region 地址带宽
        
string strCPUAddressWidth = string.Empty;

        
public string CPUAddressWidth
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strCPUAddressWidth; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strCPUAddressWidth = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
使用百分比#region 使用百分比
        
float fCPUUsedPercent;

        
public float CPUUsedPercent
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return fCPUUsedPercent; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { fCPUUsedPercent = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
CPU温度#region CPU温度
        
double strCPUTemperature;

        
public double CPUTemperature
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strCPUTemperature; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strCPUTemperature = value; }
        }

        
#endregion

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
构造函数#region 构造函数
        
public CPUInfo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//GetCPUInfo();
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetCPUInfo#region GetCPUInfo
        
public void GetCPUInfo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ContractedSubBlock.gifExpandedSubBlockStart.gif            
使用百分比#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)
ExpandedSubBlockStart.gifContractedSubBlock.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();  //获取地址带宽
            }


        }


        
public void GetCPUCurrentTemperature()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            ManagementObjectSearcher mos 
= new ManagementObjectSearcher(@"root\wmi"@"select * from MSAcpi_ThermalZoneTemperature");
            
foreach (ManagementObject mo in mos.Get())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                CPUTemperature 
= Convert.ToDouble(Convert.ToDouble(mo["CurrentTemperature"].ToString()) - 2732/ 10;
            }

        }


        
public void GetCPULoadPercentage()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            ManagementObjectSearcher searcher 
=
                    
new ManagementObjectSearcher("root\\CIMV2",
                    
"SELECT * FROM Win32_Processor");

            
foreach (ManagementObject queryObj in searcher.Get())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                CPUUsedPercent 
= float.Parse(queryObj["LoadPercentage"].ToString());
            }

        }

        
#endregion

    }

        然后在HostPerformance添加一个窗体,命名为:CPUInfoForm,代码如下:

ContractedBlock.gif ExpandedBlockStart.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/lxcsmallcity/archive/2009/11/12/1601690.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值