本文的例子在以下環(huán)境調試通過:windows2003+amd64雙核cpu+visualstudio2005(c#)下調試通過,無錯版!

首先要添加“引用”一個dll,選擇“system management”;
再引入2個命名空間:
using system.management;
using system.io;
foreach循環(huán):聲明一個迭代變量自動獲取數(shù)組中每個元素的值。
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 = "當前系統(tǒng)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的產(chǎn)品名稱
string myinfo = "計算機cpu的產(chǎn)品名稱是:";
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數(shù)據(jù)寬度
string myinfo = "當前計算機的cpu數(shù)據(jù)寬度是:";
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的產(chǎn)品名稱";
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數(shù)據(jù)寬度";
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;
}
}
新聞熱點
疑難解答