Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm looking for a way to get unique computer ID.

According to this post I can't use processor ID for this purpose. Can I take motherboard ID? What is the best way to identify the computer?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
943 views
Welcome To Ask or Share your Answers For Others

1 Answer

Like you've said CPU Id wont be unique, however you can use it with another hardware identifier to create your own unique key.

Reference assembly System.Management

So, use this code to get the CPU ID:

string cpuInfo = string.Empty;
ManagementClass mc = new ManagementClass("win32_processor");
ManagementObjectCollection moc = mc.GetInstances();

foreach (ManagementObject mo in moc)
{
     cpuInfo = mo.Properties["processorID"].Value.ToString();
     break;
}

Then use this code to get the HD ID:

string drive = "C";
ManagementObject dsk = new ManagementObject(
    @"win32_logicaldisk.deviceid=""" + drive + @":""");
dsk.Get();
string volumeSerial = dsk["VolumeSerialNumber"].ToString();

Then, you can just combine these two serials to get a uniqueId for that machine:

string uniqueId = cpuInfo + volumeSerial;

Obviously, the more hardware components you get the IDs of, the greater the uniqueness becomes. However, the chances of the same machine having an identical CPU serial and Hard disk serial are already slim to none.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...