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