本文的例子在以下环境调试通过: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);
}
}
}
form1.designer.cs
namespace windowsapplication1
{
partial class form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.icontainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void dispose(bool disposing)
{
if (disposing && (components != null))
{
components.dispose();
}
base.dispose(disposing);
}
#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void initializecomponent()
{
this.button1 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.button3 = new system.windows.forms.button();
this.button4 = new system.windows.forms.button();
this.button5 = new system.windows.forms.button();
this.button6 = new system.windows.forms.button();
this.button7 = new system.windows.forms.button();
this.button8 = new system.windows.forms.button();
this.button9 = new system.windows.forms.button();
this.button10 = new system.windows.forms.button();
this.button11 = new system.windows.forms.button();
this.button14 = new system.windows.forms.button();
this.label1 = new system.windows.forms.label();
this.suspendlayout();
//
// button1
//
this.button1.location = new system.drawing.point(12, 12);
this.button1.name = "button1";
this.button1.size = new system.drawing.size(182, 23);
this.button1.tabindex = 5;
this.button1.text = "获取cpu编号";
this.button1.usevisualstylebackcolor = true;
this.button1.click += new system.eventhandler(this.button1_click);
//
// button2
//
this.button2.location = new system.drawing.point(12, 41);
this.button2.name = "button2";
this.button2.size = new system.drawing.size(186, 23);
this.button2.tabindex = 17;
this.button2.text = "获取计算机cpu的当前电压";
this.button2.usevisualstylebackcolor = true;
this.button2.click += new system.eventhandler(this.button2_click);
//
// button3
//
this.button3.location = new system.drawing.point(12, 70);
this.button3.name = "button3";
this.button3.size = new system.drawing.size(186, 23);
this.button3.tabindex = 18;
this.button3.text = "获取计算机cpu的外部频率";
this.button3.usevisualstylebackcolor = true;
this.button3.click += new system.eventhandler(this.button3_click);
//
// button4
//
this.button4.location = new system.drawing.point(12, 99);
this.button4.name = "button4";
this.button4.size = new system.drawing.size(186, 23);
this.button4.tabindex = 19;
this.button4.text = "获取计算机cpu的二级缓存";
this.button4.usevisualstylebackcolor = true;
this.button4.click += new system.eventhandler(this.button4_click);
//
// button5
//
this.button5.location = new system.drawing.point(12, 128);
this.button5.name = "button5";
this.button5.size = new system.drawing.size(186, 23);
this.button5.tabindex = 21;
this.button5.text = "获取计算机cpu的制造商名称";
this.button5.usevisualstylebackcolor = true;
this.button5.click += new system.eventhandler(this.button5_click);
//
// button6
//
this.button6.location = new system.drawing.point(204, 157);
this.button6.name = "button6";
this.button6.size = new system.drawing.size(206, 23);
this.button6.tabindex = 22;
this.button6.text = "获取计算机cpu的产品名称";
this.button6.usevisualstylebackcolor = true;
this.button6.click += new system.eventhandler(this.button6_click);
//
// button7
//
this.button7.location = new system.drawing.point(12, 158);
this.button7.name = "button7";
this.button7.size = new system.drawing.size(186, 23);
this.button7.tabindex = 23;
this.button7.text = "获取计算机cpu的版本信息";
this.button7.usevisualstylebackcolor = true;
this.button7.click += new system.eventhandler(this.button7_click);
//
// button8
//
this.button8.location = new system.drawing.point(204, 70);
this.button8.name = "button8";
this.button8.size = new system.drawing.size(204, 23);
this.button8.tabindex = 24;
this.button8.text = "获取计算机cpu的当前使用百分比";
this.button8.usevisualstylebackcolor = true;
this.button8.click += new system.eventhandler(this.button8_click);
//
// button9
//
this.button9.location = new system.drawing.point(204, 41);
this.button9.name = "button9";
this.button9.size = new system.drawing.size(204, 23);
this.button9.tabindex = 25;
this.button9.text = "获取计算机cpu的最大时钟频率";
this.button9.usevisualstylebackcolor = true;
this.button9.click += new system.eventhandler(this.button9_click);
//
// button10
//
this.button10.location = new system.drawing.point(202, 12);
this.button10.name = "button10";
this.button10.size = new system.drawing.size(204, 23);
this.button10.tabindex = 26;
this.button10.text = "获取计算机cpu的当前时钟频率";
this.button10.usevisualstylebackcolor = true;
this.button10.click += new system.eventhandler(this.button10_click);
//
// button11
//
this.button11.location = new system.drawing.point(204, 99);
this.button11.name = "button11";
this.button11.size = new system.drawing.size(204, 23);
this.button11.tabindex = 27;
this.button11.text = "获取计算机的cpu地址宽度";
this.button11.usevisualstylebackcolor = true;
this.button11.click += new system.eventhandler(this.button11_click);
//
// button14
//
this.button14.location = new system.drawing.point(204, 128);
this.button14.name = "button14";
this.button14.size = new system.drawing.size(204, 23);
this.button14.tabindex = 28;
this.button14.text = "获取计算机的cpu数据宽度";
this.button14.usevisualstylebackcolor = true;
this.button14.click += new system.eventhandler(this.button14_click);
//
// label1
//
this.label1.autosize = true;
this.label1.location = new system.drawing.point(15, 193);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(341, 12);
this.label1.tabindex = 29;
this.label1.text = "作者:清清月儿 http://blog.csdn.net/21aspnet/ 2007.3.23";
//
// form1
//
this.autoscaledimensions = new system.drawing.sizef(6f, 12f);
this.autoscalemode = system.windows.forms.autoscalemode.font;
this.clientsize = new system.drawing.size(422, 214);
this.controls.add(this.label1);
this.controls.add(this.button14);
this.controls.add(this.button11);
this.controls.add(this.button10);
this.controls.add(this.button9);
this.controls.add(this.button8);
this.controls.add(this.button7);
this.controls.add(this.button6);
this.controls.add(this.button5);
this.controls.add(this.button4);
this.controls.add(this.button3);
this.controls.add(this.button2);
this.controls.add(this.button1);
this.name = "form1";
this.text = "form1";
this.resumelayout(false);
this.performlayout();
}
#endregion
private system.windows.forms.button button1;
private system.windows.forms.button button2;
private system.windows.forms.button button3;
private system.windows.forms.button button4;
private system.windows.forms.button button5;
private system.windows.forms.button button6;
private system.windows.forms.button button7;
private system.windows.forms.button button8;
private system.windows.forms.button button9;
private system.windows.forms.button button10;
private system.windows.forms.button button11;
private system.windows.forms.button button14;
private system.windows.forms.label label1;
}
}
新闻热点
疑难解答