c#擷取硬體資訊

來源:互聯網
上載者:User

取硬碟序號
網上流傳的方法:
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.