string strCpuID = "111";
            try
            {
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();               
                foreach (ManagementObject mo in moc)
                {
                    strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                    break;
                }
               
            }
            catch
            {
               
            }
            this.textBox1.Text = strCpuID;
网上的代码,不知道为什么在我机器上获取不了cpu序列号?高手指教

解决方案 »

  1.   

    是不是没有添加对System.Management的引用?
      

  2.   

    如果不加catch的话,报错是未将对象引用到实例也就是这句代码出错  strCpuID = mo.Properties["ProcessorId"].Value.ToString(); 
    加了catch以后就不报错了
      

  3.   

    命名空间:System.Management?你们是在vs上调试的吗?为什么没有这个命名空间
    请教并学习...
      

  4.   

    System.Management??
    如果加了这个命名空间还错的话,那就郁闷了。
    我上次也是 什么都对。自己机器上就是提示
    ProcessorId 这里 未将对象引用到实例
    但是换个机器 就可以了。
    可郁闷了
      

  5.   

    你得不到序列号的机器CPU是什么的?
      

  6.   

    ManagementClass mc = new ManagementClass("Win32_Processor"); 
                    ManagementObjectCollection moc = mc.GetInstances(); 加一句 MessageBox.Show(moc.Count.ToString());
    看看是不是1
      

  7.   


    foreach (ManagementObject mo in moc)
                    {
                        foreach (PropertyData property in mo.Properties)
                        {
                            Console.Write(property.Name + " --- ");
                            try
                            {
                                Console.WriteLine(mo.Properties[property.Name].Value.ToString());
                            }
                            catch
                            {
                                Console.WriteLine();
                            }
                        }
                        break;
                    }你看看输出有没有这一项,后面有没有数据,好像真的是有的可以有的不可以.
      

  8.   

    这为大哥,我试了,可是,什么都没有输出,我的程序是这样
    ManagementObjectSearcher moc = new ManagementObjectSearcher("select * from Win32_baseboard");
                 foreach (ManagementObject mo in moc.Get())
                 {
                     foreach (PropertyData property in mo.Properties)
                     {
                         Console.Write(property.Name + " --- ");
                         try
                         {
                             Console.WriteLine(mo.Properties[property.Name].Value.ToString());
                         }
                         catch
                         {
                             Console.WriteLine();
                         }
                     }
                     break;
                 }有一点变化在第一个foreach语句,
      

  9.   

    你这个程序是检测主板的,我的输出为
    Caption --- 底板
    ConfigOptions --- 
    CreationClassName --- Win32_BaseBoard
    Depth --- 
    Description --- 底板
    Height --- 
    HostingBoard --- True
    HotSwappable --- 
    InstallDate --- 
    Manufacturer --- ASUSTeK Computer INC.
    Model --- 
    Name --- 底板
    OtherIdentifyingInfo --- 
    PartNumber --- 
    PoweredOn --- True
    Product --- P4P800-X
    Removable --- 
    Replaceable --- 
    RequirementsDescription --- 
    RequiresDaughterBoard --- 
    SerialNumber --- MB-1234567890
    SKU --- 
    SlotLayout --- 
    SpecialRequirements --- 
    Status --- 
    Tag --- Base Board
    Version --- Rev 1.xx
    Weight --- 
    Width --- 原来的程序是测CPU的,输出为
    AddressWidth --- 32
    Architecture --- 0
    Availability --- 3
    Caption --- x86 Family 15 Model 4 Stepping 1
    ConfigManagerErrorCode --- 
    ConfigManagerUserConfig --- 
    CpuStatus --- 1
    CreationClassName --- Win32_Processor
    CurrentClockSpeed --- 2399
    CurrentVoltage --- 14
    DataWidth --- 32
    Description --- x86 Family 15 Model 4 Stepping 1
    DeviceID --- CPU0
    ErrorCleared --- 
    ErrorDescription --- 
    ExtClock --- 133
    Family --- 2
    InstallDate --- 
    L2CacheSize --- 0
    L2CacheSpeed --- 
    LastErrorCode --- 
    Level --- 15
    LoadPercentage --- 10
    Manufacturer --- GenuineIntel
    MaxClockSpeed --- 2399
    Name ---                 Intel(R) Celeron(R) CPU 2.40GHz
    NumberOfCores --- 1
    NumberOfLogicalProcessors --- 1
    OtherFamilyDescription --- 
    PNPDeviceID --- 
    PowerManagementCapabilities --- 
    PowerManagementSupported --- False
    ProcessorId --- BFEBFBFF00000F41
    ProcessorType --- 3
    Revision --- 1025
    Role --- CPU
    SocketDesignation --- CPU 1
    Status --- OK
    StatusInfo --- 3
    Stepping --- 1
    SystemCreationClassName --- Win32_ComputerSystem
    SystemName --- SUNXU
    UniqueId --- 
    UpgradeMethod --- 15
    Version --- 型号 4,步进 1
    VoltageCaps --- 输出是在 视图->输出 中查看的. 如果还没有,你看看系统属性中是不是有驱动没有安装正确啊.
      

  10.   

    没有using System.Management引用吧
      

  11.   


      string strCpuID = "111"; 
                try 
                { 
                    ManagementClass mc = new ManagementClass("Win32_Processor"); 
                    ManagementObjectCollection moc = mc.GetInstances();               
                    foreach (ManagementObject mo in moc) 
                    { 
                        strCpuID = mo.Properties["ProcessorId"].Value.ToString(); 
                        break; 
                    } 
                  
                } 
                catch 
                { 
                  
                } 
                this.textBox1.Text = strCpuID;