当前位置:编程学习 > C#/ASP.NET >>

C#获取CPU编号

我想获取CPU编号 方法是这样的:

        /// <summary>
        /// 获取CPU
        /// </summary>
        /// <returns></returns>
        public string getCPUID()
        {
            string cpuInfo = "";//cpu序列号
            ManagementClass cimobject = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = cimobject.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
            }
            return cpuInfo;
        }
为什么在不同电脑上,获取到得CPU编号是一样的。。。CPU应该不是一样的.这个方法是不是不正确。。
这个方法获取到得值是32位的,而我下了一个获取CPU编号的软件获取到得是64位的  谁能帮我解决下。。谢谢了!急!
QQ:280433796 非常感谢
--------------------编程问答-------------------- 过来学习 --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- 等待中。。。 --------------------编程问答-------------------- 楼主看下面一个博客包你满意:
http://blog.sina.com.cn/s/blog_5b2d74130100b16z.html --------------------编程问答-------------------- 给以一段源码,获取cpu号的方法,直接调用

/// <summary>
        /// 获得CUP编号
        /// </summary>
        /// <returns>获得CUP编号</returns>
        public String GetCpuID()
        {
            try
            {
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();

                String strCpuID = null;
                foreach (ManagementObject mo in moc)
                {
                    strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                    break;
                }
                return strCpuID;
            }
            catch
            {
                return "";
            }

        }


试试看啦,我是可以用的 --------------------编程问答-------------------- 顶顶顶 --------------------编程问答-------------------- --------------------编程问答-------------------- http://download.csdn.net/source/1071987

这是完整程序实例 --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- 为什么在不同电脑上,获取到得CPU编号是一样的?

现很多时候相同的CPU你得到的是相同的型号,CPU ID 现在不像以前那样是唯一的了。 你还是用网卡的 Mac 地址吧。下面是 Mac 地址的代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.ComponentModel.Design;
using Microsoft.Win32;
using System.ComponentModel;
using System.Data;
using System.Runtime.InteropServices;


/// <summary>
/// 读取网卡mac地址
/// </summary>


namespace MyUtility
{   
     
    public class MacAddr
    {
        [DllImport("Iphlpapi.dll")]
        public static extern uint GetAdaptersAddresses(uint Family, uint flags, IntPtr Reserved,
            IntPtr PAdaptersAddresses, ref uint pOutBufLen);
  
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]

        public class IP_Adapter_Addresses
        {
            public uint Length;
            public uint IfIndex;
            public IntPtr Next;

            public IntPtr AdapterName;
            public IntPtr FirstUnicastAddress;
            public IntPtr FirstAnycastAddress;
            public IntPtr FirstMulticastAddress;
            public IntPtr FirstDnsServerAddress;

            public IntPtr DnsSuffix;
            public IntPtr Description;

            public IntPtr FriendlyName;

            [MarshalAs(UnmanagedType.ByValArray,
                 SizeConst = 8)]
            public Byte[] PhysicalAddress;

            public uint PhysicalAddressLength;
            public uint flags;
            public uint Mtu;
            public uint IfType;

            public uint OperStatus;

            public uint Ipv6IfIndex;
            public uint ZoneIndices;

            public IntPtr FirstPrefix;
        }
        
        private string P_NetIP = "";


        public MacAddr()
        {
            

        }
        
        public ArrayList GetMacAddressList()
        {
            ArrayList NetAdapterList = new ArrayList();
           
            IntPtr PAdaptersAddresses = new IntPtr();
            bool AdapterFound = false;

            uint pOutLen = 100;
            PAdaptersAddresses = Marshal.AllocHGlobal(100);

            uint ret =
                GetAdaptersAddresses(0, 0, (IntPtr)0, PAdaptersAddresses, ref pOutLen);

            // if 111 error, use 
            if (ret == 111)
            {
                Marshal.FreeHGlobal(PAdaptersAddresses);
                PAdaptersAddresses = Marshal.AllocHGlobal((int)pOutLen);
                ret = GetAdaptersAddresses(0, 0, (IntPtr)0, PAdaptersAddresses, ref pOutLen);
            }

            IP_Adapter_Addresses adds = new IP_Adapter_Addresses();


            IntPtr pTemp = PAdaptersAddresses;
            IntPtr pTempIP = new IntPtr();

            while (pTemp != (IntPtr)0)
            {
                Marshal.PtrToStructure(pTemp, adds);
                string adapterName = Marshal.PtrToStringAnsi(adds.AdapterName);
                string FriendlyName = Marshal.PtrToStringAuto(adds.FriendlyName);
                string tmpString = string.Empty;

                for (int i = 0; i < 6; i++)
                {
                    tmpString += string.Format("{0:X2}", adds.PhysicalAddress[i]);

                    if (i < 5)
                    {
                        tmpString += ":";
                    }
                }


                RegistryKey theLocalMachine = Registry.LocalMachine;

                RegistryKey theSystem
                    = theLocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces");
                RegistryKey theInterfaceKey = theSystem.OpenSubKey(adapterName);

                if (theInterfaceKey != null)
                {

                    string DhcpIPAddress = (string)theInterfaceKey.GetValue("DhcpIPAddress");

                    // system is using DHCP
                    if (DhcpIPAddress != null)
                    {
                        string tArray = (string)
                            theInterfaceKey.GetValue("DhcpIPAddress", theInterfaceKey);

                        string NetWorkAdapterName = "Network adapter: " + FriendlyName.ToString();
                        string NetIP =  "IP Address: " + tArray ;
                        string NetMac = "MAC Address: " + tmpString ;
                        NetAdapterList.Add(NetWorkAdapterName + "<>" + NetIP + "<>" + NetMac);
                        AdapterFound = true;
                    }

                    else
                    {
                        string[] tArray = (string[])
                            theInterfaceKey.GetValue("IPAddress", theInterfaceKey);

                        string NetWorkAdapterName = "Network adapter: " + FriendlyName.ToString();

                        P_NetIP = "";
                        for (int Interface = 0; Interface < tArray.Length; Interface++)
                        {
                            P_NetIP += "IP Address: " + tArray[Interface];
                            AdapterFound = true;
                        }

                        string NetMac = "MAC Address: " + tmpString;
                        NetAdapterList.Add(NetWorkAdapterName + "<>" + P_NetIP + "<>" + NetMac);
                    }
                }

                pTemp = adds.Next;
            }

            if (AdapterFound != true)
            {
                //AdapterInfoTextBox.Text += "No network adapters configured/present\r\n";
            }

            return NetAdapterList;
        }
    }
}

--------------------编程问答-------------------- 学习 --------------------编程问答-------------------- 提醒LZ: CPU 是没有唯一序列号的!!!!除了N年前的奔3,
还有硬盘也是,现在大多是 SATA 硬盘,也是没有唯一序列号的,(IDE硬盘有)
所以如果LZ是想确定电脑的唯一性的话 , 最好是打网卡物理地址和主板的主意(但非品牌机的主板好象很多也没有唯一性) --------------------编程问答-------------------- using System.Management;
string GetCpuID()
          {
              try
              {
                  //获取CPU序列号代码 
                  string cpuInfo = "";//cpu序列号 
                  ManagementClass mc = new ManagementClass("Win32_Processor");
                  ManagementObjectCollection moc = mc.GetInstances();
                  foreach (ManagementObject mo in moc)
                  {
                      cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                  }
                  moc = null;
                  mc = null;
                  return cpuInfo;
              }
              catch
              {
                  return "unknow";
              }
              finally
              {
              }

          } --------------------编程问答-------------------- 新手来学习下. --------------------编程问答-------------------- 新手学习下。 --------------------编程问答--------------------     [StructLayout(LayoutKind.Sequential)]
    public struct CPU_INFO
    {
        public uint dwOemId;                           //CPU的OEM-ID
        public uint dwPageSize;                        //CPU中的页面大小
        public uint lpMinimumApplicationAddress;
        public uint lpMaximumApplicationAddress;
        public uint dwActiveProcessorMask;
        public uint dwNumberOfProcessors;              //多少个CPU
        public uint dwProcessorType;                   //CPU类型
        public uint dwAllocationGranularity;
        public uint dwProcessorLevel;                  //CPU等级
        public uint dwProcessorRevision;
    }


  [DllImport("kernel32.dll")]  
        public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);


  CPU_INFO _CpuInfo = new CPU_INFO();
            Win32API.GetSystemInfo(ref _CpuInfo);
 _CpuInfo.dwProcessorLevel.ToString(); --------------------编程问答-------------------- 学习学习 --------------------编程问答-------------------- 建议使用硬盘序列号作为唯一标识 --------------------编程问答-------------------- 之前我也需要用到过这种功能,后来网上下载了一段代码解决的。 --------------------编程问答-------------------- 取多个硬件的序列号组合一下更好.
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,