日期:2011-07-02 浏览次数:20464 次
本文的例子在以下环境调试通过: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