djlzxzy

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

http://hi.baidu.com/jackeyrain/blog/item/437bed7f706fa20328388a9f.html 

关于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);

        }
    }
}    

posted on 2011-08-10 22:25  djlzxzy  阅读(492)  评论(0编辑  收藏  举报