C#实现读取CPU编号厂商主频电压等全部信息

Form1.Designer.cs:

  1. namespace CPUInfo
  2. {
  3.     partial class Form1
  4.     {
  5.         /// <summary>
  6.         /// 必需的设计器变量。
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;

  9.         /// <summary>
  10.         /// 清理所有正在使用的资源。
  11.         /// </summary>
  12.         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  13.         protected override void Dispose(bool disposing)
  14.         {
  15.             if (disposing && (components != null))
  16.             {
  17.                 components.Dispose();
  18.             }
  19.             base.Dispose(disposing);
  20.         }

  21.         #region Windows 窗体设计器生成的代码

  22.         /// <summary>
  23.         /// 设计器支持所需的方法 - 不要
  24.         /// 使用代码编辑器修改此方法的内容。
  25.         /// </summary>
  26.         private void InitializeComponent()
  27.         {
  28.             this.button1 = new System.Windows.Forms.Button();
  29.             this.button2 = new System.Windows.Forms.Button();
  30.             this.button3 = new System.Windows.Forms.Button();
  31.             this.button4 = new System.Windows.Forms.Button();
  32.             this.button5 = new System.Windows.Forms.Button();
  33.             this.button6 = new System.Windows.Forms.Button();
  34.             this.button7 = new System.Windows.Forms.Button();
  35.             this.button8 = new System.Windows.Forms.Button();
  36.             this.button9 = new System.Windows.Forms.Button();
  37.             this.button10 = new System.Windows.Forms.Button();
  38.             this.button11 = new System.Windows.Forms.Button();
  39.             this.button12 = new System.Windows.Forms.Button();
  40.             this.button13 = new System.Windows.Forms.Button();
  41.             this.button14 = new System.Windows.Forms.Button();
  42.             this.label1 = new System.Windows.Forms.Label();
  43.             this.SuspendLayout();
  44.             // 
  45.             // button1
  46.             // 
  47.             this.button1.Location = new System.Drawing.Point(12, 12);
  48.             this.button1.Name = "button1";
  49.             this.button1.Size = new System.Drawing.Size(182, 23);
  50.             this.button1.TabIndex = 5;
  51.             this.button1.Text = "获取CPU编号";
  52.             this.button1.UseVisualStyleBackColor = true;
  53.             this.button1.Click += new System.EventHandler(this.button1_Click);
  54.             // 
  55.             // button2
  56.             // 
  57.             this.button2.Location = new System.Drawing.Point(12, 41);
  58.             this.button2.Name = "button2";
  59.             this.button2.Size = new System.Drawing.Size(186, 23);
  60.             this.button2.TabIndex = 17;
  61.             this.button2.Text = "获取计算机CPU的当前电压";
  62.             this.button2.UseVisualStyleBackColor = true;
  63.             this.button2.Click += new System.EventHandler(this.button2_Click);
  64.             // 
  65.             // button3
  66.             // 
  67.             this.button3.Location = new System.Drawing.Point(12, 70);
  68.             this.button3.Name = "button3";
  69.             this.button3.Size = new System.Drawing.Size(186, 23);
  70.             this.button3.TabIndex = 18;
  71.             this.button3.Text = "获取计算机CPU的外部频率";
  72.             this.button3.UseVisualStyleBackColor = true;
  73.             this.button3.Click += new System.EventHandler(this.button3_Click);
  74.             // 
  75.             // button4
  76.             // 
  77.             this.button4.Location = new System.Drawing.Point(12, 99);
  78.             this.button4.Name = "button4";
  79.             this.button4.Size = new System.Drawing.Size(186, 23);
  80.             this.button4.TabIndex = 19;
  81.             this.button4.Text = "获取计算机CPU的二级缓存";
  82.             this.button4.UseVisualStyleBackColor = true;
  83.             this.button4.Click += new System.EventHandler(this.button4_Click);
  84.             // 
  85.             // button5
  86.             // 
  87.             this.button5.Location = new System.Drawing.Point(12, 128);
  88.             this.button5.Name = "button5";
  89.             this.button5.Size = new System.Drawing.Size(186, 23);
  90.             this.button5.TabIndex = 21;
  91.             this.button5.Text = "获取计算机CPU的制造商名称";
  92.             this.button5.UseVisualStyleBackColor = true;
  93.             this.button5.Click += new System.EventHandler(this.button5_Click);
  94.             // 
  95.             // button6
  96.             // 
  97.             this.button6.Location = new System.Drawing.Point(204, 157);
  98.             this.button6.Name = "button6";
  99.             this.button6.Size = new System.Drawing.Size(206, 23);
  100.             this.button6.TabIndex = 22;
  101.             this.button6.Text = "获取计算机CPU的产品名称";
  102.             this.button6.UseVisualStyleBackColor = true;
  103.             // 
  104.             // button7
  105.             // 
  106.             this.button7.Location = new System.Drawing.Point(12, 158);
  107.             this.button7.Name = "button7";
  108.             this.button7.Size = new System.Drawing.Size(186, 23);
  109.             this.button7.TabIndex = 23;
  110.             this.button7.Text = "获取计算机CPU的版本信息";
  111.             this.button7.UseVisualStyleBackColor = true;
  112.             this.button7.Click += new System.EventHandler(this.button7_Click);
  113.             // 
  114.             // button8
  115.             // 
  116.             this.button8.Location = new System.Drawing.Point(204, 70);
  117.             this.button8.Name = "button8";
  118.             this.button8.Size = new System.Drawing.Size(204, 23);
  119.             this.button8.TabIndex = 24;
  120.             this.button8.Text = "获取计算机CPU的当前使用百分比";
  121.             this.button8.UseVisualStyleBackColor = true;
  122.             // 
  123.             // button9
  124.             // 
  125.             this.button9.Location = new System.Drawing.Point(204, 41);
  126.             this.button9.Name = "button9";
  127.             this.button9.Size = new System.Drawing.Size(204, 23);
  128.             this.button9.TabIndex = 25;
  129.             this.button9.Text = "获取计算机CPU的最大时钟频率";
  130.             this.button9.UseVisualStyleBackColor = true;
  131.             // 
  132.             // button10
  133.             // 
  134.             this.button10.Location = new System.Drawing.Point(202, 12);
  135.             this.button10.Name = "button10";
  136.             this.button10.Size = new System.Drawing.Size(204, 23);
  137.             this.button10.TabIndex = 26;
  138.             this.button10.Text = "获取计算机CPU的当前时钟频率";
  139.             this.button10.UseVisualStyleBackColor = true;
  140.             // 
  141.             // button11
  142.             // 
  143.             this.button11.Location = new System.Drawing.Point(204, 99);
  144.             this.button11.Name = "button11";
  145.             this.button11.Size = new System.Drawing.Size(204, 23);
  146.             this.button11.TabIndex = 27;
  147.             this.button11.Text = "获取计算机的CPU地址宽度";
  148.             this.button11.UseVisualStyleBackColor = true;
  149.             // 
  150.             // button12
  151.             // 
  152.             this.button12.Location = new System.Drawing.Point(0, 0);
  153.             this.button12.Name = "button12";
  154.             this.button12.Size = new System.Drawing.Size(75, 23);
  155.             this.button12.TabIndex = 0;
  156.             // 
  157.             // button13
  158.             // 
  159.             this.button13.Location = new System.Drawing.Point(0, 0);
  160.             this.button13.Name = "button13";
  161.             this.button13.Size = new System.Drawing.Size(75, 23);
  162.             this.button13.TabIndex = 0;
  163.             // 
  164.             // button14
  165.             // 
  166.             this.button14.Location = new System.Drawing.Point(204, 128);
  167.             this.button14.Name = "button14";
  168.             this.button14.Size = new System.Drawing.Size(204, 23);
  169.             this.button14.TabIndex = 28;
  170.             this.button14.Text = "获取计算机的CPU数据宽度";
  171.             this.button14.UseVisualStyleBackColor = true;
  172.             // 
  173.             // label1
  174.             // 
  175.             this.label1.AutoSize = true;
  176.             this.label1.Location = new System.Drawing.Point(15, 193);
  177.             this.label1.Name = "label1";
  178.             this.label1.Size = new System.Drawing.Size(77, 12);
  179.             this.label1.TabIndex = 29;
  180.             this.label1.Text = "作者:范中磊";
  181.             // 
  182.             // Form1
  183.             // 
  184.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  185.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  186.             this.ClientSize = new System.Drawing.Size(422, 214);
  187.             this.Controls.Add(this.label1);
  188.             this.Controls.Add(this.button14);
  189.             this.Controls.Add(this.button11);
  190.             this.Controls.Add(this.button10);
  191.             this.Controls.Add(this.button9);
  192.             this.Controls.Add(this.button8);
  193.             this.Controls.Add(this.button7);
  194.             this.Controls.Add(this.button6);
  195.             this.Controls.Add(this.button5);
  196.             this.Controls.Add(this.button4);
  197.             this.Controls.Add(this.button3);
  198.             this.Controls.Add(this.button2);
  199.             this.Controls.Add(this.button1);
  200.             this.Name = "Form1";
  201.             this.Text = "Form1";
  202.             this.Load += new System.EventHandler(this.Form1_Load);
  203.             this.ResumeLayout(false);
  204.             this.PerformLayout();

  205.         }

  206.         #endregion

  207.         private System.Windows.Forms.Button button1;
  208.         private System.Windows.Forms.Button button2;
  209.         private System.Windows.Forms.Button button3;
  210.         private System.Windows.Forms.Button button4;
  211.         private System.Windows.Forms.Button button5;
  212.         private System.Windows.Forms.Button button6;
  213.         private System.Windows.Forms.Button button7;
  214.         private System.Windows.Forms.Button button8;
  215.         private System.Windows.Forms.Button button9;
  216.         private System.Windows.Forms.Button button10;
  217.         private System.Windows.Forms.Button button11;
  218.         private System.Windows.Forms.Button button12;
  219.         private System.Windows.Forms.Button button13;
  220.         private System.Windows.Forms.Button button14;
  221.         private System.Windows.Forms.Label label1;
  222.     }
  223. }

Form1.cs:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Management;
  9. using System.IO;

  10. namespace CPUInfo
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }

  18.         private void Form1_Load(object sender, EventArgs e)
  19.         {

  20.         }

  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             ManagementClass MyClass = new ManagementClass("Win32_Processor");
  24.             ManagementObjectCollection MyCollection = MyClass.GetInstances();
  25.             String MyInfo = "当前系统CPU编号是:";
  26.             string MyCPUID = "";
  27.             foreach (ManagementObject MyObject in MyCollection)
  28.             {
  29.                 MyCPUID = MyObject.Properties["ProcessorId"].Value.ToString();
  30.                 break;
  31.             }
  32.             MyInfo += MyCPUID;
  33.             MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  34.         }

  35.         private void button2_Click(object sender, EventArgs e)
  36.         {
  37.             //获取计算机CPU的当前电压
  38.             String MyInfo = "计算机CPU的当前电压是:";
  39.             ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  40.             foreach (ManagementObject MyObject in MySearcher.Get())
  41.             {
  42.                 try
  43.                 {
  44.                     MyInfo += "\n" + String.Format("CurrentVoltage : " + MyObject["CurrentVoltage"].ToString());
  45.                     MyInfo += "\n=========================================================";
  46.                 }
  47.                 catch { }
  48.             }
  49.             MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  50.         }

  51.         private void button3_Click(object sender, EventArgs e)
  52.         {
  53.             //获取计算机CPU的外部频率
  54.             String MyInfo = "计算机CPU的外部频率是:";
  55.             ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  56.             foreach (ManagementObject MyObject in MySearcher.Get())
  57.             {
  58.                 try
  59.                 {
  60.                     MyInfo += "\n" + String.Format("ExtClock : " + MyObject["ExtClock"].ToString());
  61.                     MyInfo += "\n=========================================================";
  62.                 }
  63.                 catch { }
  64.             }
  65.             MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  66.         }

  67.         private void button4_Click(object sender, EventArgs e)
  68.         {
  69.             //获取计算机CPU的二级缓存
  70.             String MyInfo = "计算机CPU的二级缓存尺寸是:";
  71.             ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  72.             foreach (ManagementObject MyObject in MySearcher.Get())
  73.             {
  74.                 MyInfo += "\n" + String.Format("L2CacheSize: " + MyObject["L2CacheSize"].ToString());
  75.                 MyInfo += "\n=========================================================";
  76.             }
  77.             MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  78.         }

  79.         private void button5_Click(object sender, EventArgs e)
  80.         {
  81.             //获取计算机CPU的制造商名称
  82.             String MyInfo = "计算机CPU的制造商名称是:";
  83.             ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  84.             foreach (ManagementObject MyObject in MySearcher.Get())
  85.             {
  86.                 MyInfo += "\n" + String.Format("Manufacturer : " + MyObject["Manufacturer"].ToString());
  87.                 MyInfo += "\n=========================================================";
  88.             }
  89.             MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  90.         }

  91.         private void button6_Click(object sender, EventArgs e)
  92.         {
  93.             //获取计算机CPU的产品名称
  94.             String MyInfo = "计算机CPU的产品名称是:";
  95.             ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  96.             foreach (ManagementObject MyObject in MySearcher.Get())
  97.             {
  98.                 MyInfo += "\n" + String.Format("Name : " + MyObject["Name"].ToString());
  99.                 MyInfo += "\n=========================================================";
  100.             }
  101.             MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

  102.         }

  103.         private void button7_Click(object sender, EventArgs e)
  104.         {
  105.             //获取计算机CPU的版本信息
  106.             String MyInfo = "计算机CPU的版本信息如下:";
  107.             ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  108.             foreach (ManagementObject MyObject in MySearcher.Get())
  109.             {
  110.                 MyInfo += "\n" + String.Format("Version: " + MyObject["Version"].ToString());
  111.                 MyInfo += "\n=========================================================";
  112.             }
  113.             MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

  114.         }

  115.         private void button8_Click(object sender, EventArgs e)
  116.         {
  117.             //获取计算机CPU的当前使用百分比 注意要把SQLserver或者其他耗CPU的软件开着否则看不到效果就一直为0
  118.             String MyInfo = "计算机CPU的当前使用百分比是:";
  119.             ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  120.             foreach (ManagementObject MyObject in MySearcher.Get())
  121.             {
  122.                 MyInfo += "\n" + String.Format("LoadPercentage : " + MyObject["LoadPercentage"].ToString());
  123.                 MyInfo += "\n=========================================================";
  124.             }
  125.             MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

  126.         }

  127.         private void button9_Click(object sender, EventArgs e)
  128.         {
  129.             //获取计算机CPU的最大时钟频率
  130.             String MyInfo = "计算机CPU的最大时钟频率是:";
  131.             ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  132.             foreach (ManagementObject MyObject in MySearcher.Get())
  133.             {
  134.                 MyInfo += "\n" + String.Format("MaxClockSpeed : " + MyObject["MaxClockSpeed"].ToString());
  135.                 MyInfo += "\n=========================================================";
  136.             }
  137.             MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

  138.         }

  139.         private void button10_Click(object sender, EventArgs e)
  140.         {
  141.             //获取计算机CPU的当前时钟频率
  142.             String MyInfo = "计算机CPU的当前时钟频率是:";
  143.             ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  144.             foreach (ManagementObject MyObject in MySearcher.Get())
  145.             {
  146.                 MyInfo += "\n" + String.Format("CurrentClockSpeed : " + MyObject["CurrentClockSpeed"].ToString());
  147.                 MyInfo += "\n=========================================================";
  148.             }
  149.             MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

  150.         }

  151.         private void button11_Click(object sender, EventArgs e)
  152.         {
  153.             //获取计算机的CPU地址宽度
  154.             String MyInfo = "当前计算机的CPU地址宽度是:";
  155.             ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  156.             foreach (ManagementObject MyObject in MySearcher.Get())
  157.             {
  158.                 MyInfo += "\n" + String.Format("AddressWidth: " + MyObject["AddressWidth"].ToString());
  159.                 MyInfo += "\n=========================================================";
  160.             }
  161.             MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

  162.         }

  163.         private void button14_Click(object sender, EventArgs e)
  164.         {
  165.             //获取计算机的CPU数据宽度
  166.             String MyInfo = "当前计算机的CPU数据宽度是:";
  167.             ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  168.             foreach (ManagementObject MyObject in MySearcher.Get())
  169.             {
  170.                 MyInfo += "\n" + String.Format("DataWidth : " + MyObject["DataWidth"].ToString());
  171.                 MyInfo += "\n=========================================================";
  172.             }
  173.             MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

  174.         }
  175.     }
  176. }
posted @ 2011-03-28 10:05  ccczqh  阅读(839)  评论(0编辑  收藏  举报