c#获取硬件信息

取硬盘序列号
网上流传的方法:
public string getHardDiskID()
        {
            ManagementClass searcher = new ManagementClass("WIN32_DiskDrive");
            ManagementObjectCollection moc = searcher.GetInstances();
            string strHardDiskID = "";
            foreach (ManagementObject mo in moc)
            {
                strHardDiskID = mo["Model"].ToString().Trim();
                break;
            }
            return strHardDiskID;
        }
是错的,该方法取到的是硬盘的型号,并不是序列号,msdn上的描述:Manufacturer's model number of the disk drive.
正确方法:
     ManagementClass searcher = new ManagementClass("WIN32_PhysicalMedia");
            ManagementObjectCollection moc = searcher.GetInstances();
            //硬盘序列号
            string strHardDiskID = "";
            foreach (ManagementObject mo in moc)
            {
                try
                {
                    strHardDiskID += mo["SerialNumber"].ToString().Trim();
                    break;
                }
                catch
                {
      //有可能是u盘之类的东西
  }
            }
msdn的描述:Manufacturer-allocated number used to identify the physical media,该号为厂商分配的标志,也就是说没有规定它必须是唯一的,只是厂商的标志而已,但是对于硬盘来说该号作为序列号的重复的可能性很小,除非有两家硬盘厂商的命名规则一模一样,


取cpu编号:
网上流传的方法:
public string getCPUID()
        {
            ManagementClass searcher = new ManagementClass("WIN32_Processor");
            ManagementObjectCollection moc = searcher.GetInstances();
            string strCPUID = "";
            foreach (ManagementObject mo in moc)
            {
                strCPUID = mo["ProcessorId"].ToString().Trim();
                break;
            }
            return strCPUID;
        }
也是错的,
msdn上的描述:Processor information that describes the processor features. For an x86 class CPU, the field format depends on the processor support of the CPUID instruction. If the instruction is supported, the property contains 2 (two) DWORD formatted values. The first is an offset of 08h-0Bh, which is the EAX value that a CPUID instruction returns with input EAX set to 1. The second is an offset of 0Ch-0Fh, which is the EDX value that the instruction returns. Only the first two bytes of the property are significant and contain the contents of the DX register at CPU reset—all others are set to 0 (zero), and the contents are in DWORD format.
它只是描述一个型号的号码而已。
正确的应该是:WIN32_Processor.UniqueId
msdn上的描述:Globally unique identifier for the processor. This identifier may only be unique within a processor family. This property is inherited from CIM_Processor.
这个号也是针对厂商唯一的,不过生产cpu的厂商………,应该不会重吧。

取网卡编号:
public string getNetID()
        {
            ManagementClass searcher = new ManagementClass("WIN32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = searcher.GetInstances();
            string strNetID = "";
            foreach (ManagementObject mo in moc)
            {
                if ((Boolean)mo["IpEnabled"])
                {
                    strNetID = mo["MacAddress"].ToString();
                    break;
                }
            }
            return strNetID;
        }
网卡号取出来的是mac地址,是唯一的,但是用户是可以修改的,而且机器可能有多个网卡,每次取出来的网卡号可能是不一样的(在多个网卡的情况下,只需要将网卡禁用再启动就可能导致取出来的mac地址不一样),

网络害人,网上流传的方法大都是这两种,同志们引以为戒!


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/nuey1985/archive/2010/01/04/5127748.aspx

你可能感兴趣的:(c#获取硬件信息)