C#精髓【月儿原创】第二讲 WMI完美秀出CPU编号厂商主频百分比等全部信息
說明:準備出一個系列,所謂精髓講C#語言要點。這個系列沒有先后順序,不過盡量做到精。可能會不斷增刪整理,本系列最原始出處是csdn博客,謝謝關注。
C#精髓
第二講 WMI完美秀出CPU編號廠商主頻電壓等全部信息
作者:清清月兒
主頁:http://blog.csdn.net/21aspnet/?????????? 時間:2007.3.23
關于WMI MSDN有詳細說明。?本文列舉數例算拋磚引玉吧。WMI是個好東西,看過本文后也許你應該能舉一反三參考MSDN也許自己做個優化大師也是可能的。
本文的例子在以下環境調試通過: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;
??? }
}
?
總結
以上是生活随笔為你收集整理的C#精髓【月儿原创】第二讲 WMI完美秀出CPU编号厂商主频百分比等全部信息的全部內容,希望文章能夠幫你解決所遇到的問題。