32

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?

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
StuffHappens
  • 6,457
  • 13
  • 70
  • 95
  • 1
    This has nothing to do with C#. – Daniel Goldberg Aug 13 '10 at 07:57
  • 14
    One would assume (rash, I appreciate, when answering programming questions) that once a generic mechanism has been identified a specific implementation in C# will be desired... – Murph Aug 13 '10 at 07:59
  • What tag should I use for this question? – StuffHappens Aug 13 '10 at 08:02
  • 1
    possible duplicate: http://stackoverflow.com/questions/3443093/what-is-a-good-unique-pc-identifier/3443148#3443148 – schoetbi Aug 13 '10 at 08:03
  • you should tag it with C# if you want an example of how to get a unique computer ID in C#... which I'm guessing is what you're after isn't it? – djdd87 Aug 13 '10 at 08:18
  • @djdd87: The asker *did* use the [tag:c#] tag, some other user who didn't read the tag wiki incorrectly applied [tag:system-identification] in its place. – Ben Voigt Jul 12 '22 at 15:35

3 Answers3

38

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.

Valamas
  • 24,169
  • 25
  • 107
  • 177
djdd87
  • 67,346
  • 27
  • 156
  • 195
  • As I said before processor ID is not appropriate. – StuffHappens Aug 13 '10 at 08:05
  • I see your point. I just doubt what will I get if there's a RAID and there are 2 different HDD with disk C on them. – StuffHappens Aug 13 '10 at 08:20
  • @StuffHappens - I'm assuming it'll probably end up returning the RAID controller ID or something. I'm sure Microsoft has considered that. – djdd87 Aug 13 '10 at 08:23
  • @StuffHappens, just hash together multiple IDs of hard disks. – Daniel Goldberg Aug 13 '10 at 09:32
  • FWIW, I built a unique id generator using processor id and disk serial number (with fallbacks), and it failed on a pair of machines we'd purchased from Dell at the same time. – Thomas Oct 26 '11 at 20:34
  • I need to use something like this from within an application that's published via ClickOnce. My end users may have rather restricted access to their PCs. Will this method cause any problems if the end-user is not an Admin on their machine? Will this work with XP/7/8? Thank you. I really need to derive a unique, local key for my project. – RLH Jan 06 '14 at 21:29
  • @GenericTypeTea I am getting an error : Attempt by security transparent method 'demo.Program.demo()' to access security critical method 'System.Management.ManagementClass..ctor(System.String)' failed. – Vishal Feb 02 '15 at 13:19
  • 10
    This does not produce a unique ID. We so far tested this on 15 machines purchased from Dell and already have 3 with the same ID. – Stefan Jul 07 '15 at 19:53
  • 1
    Maybe try more than just the two components like the answer says? – djdd87 Jul 08 '15 at 11:09
5

MAC address of the network adapter? Security identifier (SID) of the windows OS install? (assuming it's windows you're dealing with) Could you just generate a GUID for each PC?

What exactly are you trying to achieve?

AndySw
  • 341
  • 2
  • 7
  • 3
    If he's trying to do this for a licencing mechanism, then SIDs and MAC addresses can be changed very easily so wouldn't be acceptable. – djdd87 Aug 13 '10 at 08:15
4

The motherboard ID is a pretty unique identifier. Another option is to use the network cards MAC address, which are pretty much unique.

Daniel Goldberg
  • 19,908
  • 4
  • 21
  • 29
  • 6
    Should be noted however that MACs can be changed from the NIC properties window for a lot of NICs, while a motherboard ID is a lot harder to change. – ErikHeemskerk Aug 13 '10 at 07:59
  • As far as I know you can change your mac address. And what to do if there are more than one netboard (or how should I name it)? – StuffHappens Aug 13 '10 at 08:01
  • 1
    That is correct. A motherboard ID is far more unique. However, if you do not require very strong uniqueness (i.e you can assume MACs don't change), acquiring a mac address is far easier. @Stuff, hash the addresses together? :) – Daniel Goldberg Aug 13 '10 at 09:32