51

I've been looking at the code in this tutorial, and I found that it uses My.Computer.Name to save settings that shouldn't roam between computers. It's entirely possible, however, for a user to have two identically named PCs. If they wanted to have the same username on each PC, for example, they may very well end up with two PCs named Username-PC.

What are some good methods of identifying different PCs? Do PCs have GUIDs associated with them, or should I look into pulling the serial number off of some hardware? I don't care if the identification persists through reinstallation of Windows.

(The tutorial I linked is in VB.Net, but I'm implementing it in C#)

2

13 Answers 13

56

Some good identifiers:

  • MAC Address: It's fairly easy to get at, and it's usually unique. However, it can be spoofed/changed rather easily, so it depends on how unique it needs to be.
  • CPU Serial Number: It's not available on lots of older systems, but it's there. Check out this MSDN page. It won't change, but it's bound to a computer.
  • HDD Serial Number: It's likely to not change, but can be a nuisance if the HD fails. Check out this MSDN page.
8
  • 4
    +1 Yeah, be careful with the MAC. I had a ton of problems before with the MAC changing & being duplicated. PITA. Aug 9, 2010 at 18:52
  • Careful: I've seen cases where the CPU (of a laptop) got toasted and had to be replaced. New CPU, new CPUID. Aug 9, 2010 at 18:58
  • 2
    @Steven: Good point. Generally, though, I'd consider it one of the "most reliable" IDs on a system. CPU failure is relatively uncommon (compared to HD failure). It should also be noted that this number might get gummed up when you start dealing with virtualization.
    – mattbasta
    Aug 9, 2010 at 20:37
  • 1
    Another reason about avoid MAC: I think that MAC address of the first enabled card is not a good idea for laptops (I still have not solved my issues and I'm searching for a different solution). They change the card if you attach a network cable then use WiFi
    – bubi
    Jul 19, 2017 at 5:51
  • 1
    @bubi The SerialNumber property that gets the CPU serial. Look on your MSDN link and Ctrl+F "SerialNumber" and read the last note on operating systems for that property.
    – qJake
    Jul 21, 2017 at 20:40
23

If you are on windows HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\ CurrentVersion\ProductId is unique per machine/per windows install. where as in some of the other answers like the MAC address, Proc SN, and HD SN will stay the same between windows reinstalls/dual boot situations.

9
  • 1
    Is this the number used by Windows to detect when it's been installed onto a different machine? Aug 9, 2010 at 18:59
  • 2
    Well, the positive here is that anything capable of changing this product ID will also be a major pain to Windows itself. :-) Aug 9, 2010 at 19:23
  • 2
    @Rory I believe that performing a sysprep will cause that ID to change upon the first time you start the system (and if you are using system images you really should be using sysprep) Feb 14, 2013 at 18:34
  • 2
    I know it's been a while, but today I found an other answer here and thus I wonder what is the difference between ids in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\ CurrentVersion\ProductId and in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\MachineGuid ?
    – Skipper
    Apr 25, 2017 at 19:26
  • 1
    @Skipper ProductId is a unique id used with windows installs. MachineGuid is used when you use the windows encryption system when you a "Protect Data" on the "local machine" scope Aug 8, 2019 at 21:43
11

The real answer to that question: There is no such thing.

There are several "close enough" solutions, but each one of those has it's own limitation.

All the hardware IDs - Hardware changes. And, in many cases you can change those identifiers (For example, MAC spoofing).

The SID, as I've already commented, Is not that good as well, because the SID won't change if the computer was installed from an image. The SID is generated by windows installation, if windows wasn't installed, but copied from an image, the SID won't change (although it is common to regenerate it because of a myth about "security risk" - you can't count on that).

Computer name - Well, as mentioned, They suppose to be unique, but it's not enforced in any way.

Another solution you can implement is to generate you own unique identifier and store it locally (assuming you can do such thing). Again, this solution won't work if your computer was imaged with your application.

The best solution for you really depends on what you are trying to accomplish. I had the same problem with a quite large network, and the best solution in my case was the computer's name. If you are absolutely sure that your process won't be imaged, I would generate a unique identifier using Guid because it will probably be the safest.

9

Here is a way to uniquely identify a computer. Using System.Management to get Win32_BIOS, you can get unique values from your machine's BIOS.

See: Win32_BIOS class, http://msdn.microsoft.com/en-us/library/aa394077.aspx

using System.Management;

string UniqueMachineId()
{
    StringBuilder builder = new StringBuilder();

    String query = "SELECT * FROM Win32_BIOS";
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    //  This should only find one
    foreach (ManagementObject item in searcher.Get())
    {
        Object obj = item["Manufacturer"];
        builder.Append(Convert.ToString(obj));
        builder.Append(':');
        obj = item["SerialNumber"];
        builder.Append(Convert.ToString(obj));
    }

return builder.ToString();
}

With similar logic, you can also step through "Win32_DiskDrive"; http://msdn.microsoft.com/en-us/library/aa394132.aspx; and get "SerialNumber" for each physical drive. In this case, the

    foreach (ManagementObject item in searcher.Get())

should find multiple items

1
  • 6
    It's quite unsafe to use the Bios serial number since you might get a lot of these (identical!) strings: "To be filled by O.E.M.".
    – movAX13h
    Apr 7, 2015 at 18:54
8

Take three identifiers that are semi-unique and semi-constant. Use the rule that 2 out of 3 is sufficient for a positive identification. Update the registered data for the 1 out of 3 that is occasionally wrong.

1
  • This is the only right answer. They need to cover the range of potentially replaceable parts. I would use BIOS serial number, HDD Id, Windows Serial #.
    – richard
    Jul 9, 2019 at 7:05
2

Use the network card's MAC address. It's supposed to be unique. It can be changed, though. It depends on how malicious you expect your users to be and how critical your application is.

Some sample code to do it:

public string GetMACAddress() {
    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection moc = mc.GetInstances();

    string MACAddress = String.Empty;

    foreach (ManagementObject mo in moc) {
        if (MACAddress == String.Empty) { // only return MAC Address from first card
            if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
        }
        mo.Dispose();
    }

    return MACAddress;
}
1

One thing you can use is the MAC of any Network interface. You can also combine several sources of information. Like HDD Serial number, mac, processor type to calculate a hash from it.

1
  • 2
    The more sources you use, the more reliable it is. However, the more sources you use, the more vulnerable you are to false positives. Aug 9, 2010 at 18:58
0

I don't think it's possible to have two PC's with the same name on the same domain. Have you tried capturing the domain name?

3
  • 2
    I have 2 PCs on the same domain with the same name :-) I dual boot between xp and win 7...although haven't booted into xp fr over 2 weeks now
    – SQLMenace
    Aug 9, 2010 at 18:44
  • @SQLMenace: Unless that have the same SID, the network still sees them as different boxes. Aug 9, 2010 at 20:49
  • It is the same box..I had to do some fancy stuff to make it to the domain from both boxes
    – SQLMenace
    Aug 9, 2010 at 21:03
0

Take a look here: Getting Service Tag from Dell Machine using .net?

You could snatch some unique data from the registry.

2
  • Steven, these specific values are hardware specific.
    – JonH
    Aug 9, 2010 at 18:47
  • 1
    Hardware changes. Disc drives, CPU's, all of it. When it breaks, it's replaced. Aug 9, 2010 at 19:22
0

Each computer has a SID that's unique under normal circumstances.

4
  • 3
    why is this the accepted answer? What is this SID and how to obtain it?
    – Marek
    Sep 19, 2012 at 7:09
  • 3
    And it's even not correct. Please remove the mark from this answer. The SID (security identifier) is an identifier generated when you install windows. It is very unlikely that two computers will have the same SID. The SID was originally designed in order to identify users uniquely across a network. Why isn't it good enough? Because if the computer was not installed from scratch, but was installed from an image, then it will have the same SID of the original computer. This method for installing computers is very common in large networks, therefor SID is a bad choice in practice.
    – Dig
    Sep 22, 2012 at 16:58
  • @DiGMi, you're right about cloning resulting in the same SID. That's why I said "under normal circumstances". As you mentioned in your response, some companies that install clone images do regenerate the SID. Sep 27, 2012 at 20:14
  • The problem is that there is no right answer universally, just answers that work better or worse depending on circumstances. It sounds like the OP is more concerned about avoiding a false match than having a false mismatch, in which case the CPU or HD ID's would work best. Sep 27, 2012 at 20:16
0

In a managed network environment, the best, most reliable identifier might be the one you create, but there are some downsides.

Some (many?) manufacturers provide a utility that allows you to set an asset tag that is stored in the firmware. This might be a bootable utility, or it might run within Windows, or it might even be built into the firmware setup. This "tag" is an arbitrary text string that you can set to whatever you want, and then read it back using WMI and the Win32_SystemEnclosure class...

string[] selectedProperties = new string[] { "SMBIOSAssetTag" };
ObjectQuery enclosureQuery = new SelectQuery("Win32_SystemEnclosure", null, selectedProperties);

using (ManagementObjectSearcher enclosureSearcher = new ManagementObjectSearcher(enclosureQuery))
using (ManagementObjectCollection enclosureCollection = enclosureSearcher.Get())
{
    foreach (ManagementObject enclosure in enclosureCollection)
    {
        string assetTag = (string) enclosure.GetPropertyValue("SMBIOSAssetTag");
    }
}

Pros:

  • You can use whatever scheme you want (e.g. incorporating date, department, incrementing integers, GUIDs, etc.).
  • You can use one scheme for all machines regardless of their manufacturer, instead of having to handle manufacturer-specific schemes.
  • By allocating and tracking the identifiers yourself, you can guarantee that they are unique. Not relying on an identifier set by the manufacturer means there is no risk of duplicates within a manufacturer or between manufacturers.
  • The identifier is stored in the firmware — not on the hard drive — so it will survive reformatting, upgrades, etc. but also not be duplicated by backups/imaging/cloning.

Cons:

  • You need to actually set the asset tag; they'll all be blank until you do so.
  • Setting a machine's asset tag may require physical access and a reboot.
  • Asset tags are not write-once and could, therefore, be changed or erased.
    • Password-protected firmware should require that password before changing the tag, but that's not guaranteed.
  • By allocating and tracking the identifiers yourself, there's not only the overhead of...allocating and tracking the identifiers, but also the possibility that you'll introduce duplicates if you're not careful.
  • Using asset tags for this purpose requires that all machines support setting an asset tag and properly report it to WMI.
0

We use a combination of the ProcessorID from Win32_processor and the UUID from Win32_ComputerSystemProduct:

ManagementObjectCollection mbsList = null;
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select ProcessorID From Win32_processor");
mbsList = mos.Get();
string processorId = string.Empty;
foreach (ManagementBaseObject mo in mbsList)
{
    processorId = mo["ProcessorID"] as string;
}

mos = new ManagementObjectSearcher("SELECT UUID FROM Win32_ComputerSystemProduct");
mbsList = mos.Get();
string systemId = string.Empty;
foreach (ManagementBaseObject mo in mbsList)
{
    systemId = mo["UUID"] as string;
}

var compIdStr = $"{processorId}{systemId}";

Previously, we used a combination: processor ID ("Select ProcessorID From Win32_processor") and the motherboard serial number ("SELECT SerialNumber FROM Win32_BaseBoard"), but then we found out that the serial number of the motherboard may not be filled in, or it may be filled in with uniform values:

  • To be filled by O.E.M.
  • None
  • Default string

Therefore, it is worth considering this situation.

Also keep in mind that the ProcessorID number may be the same on different computers.

-1

There is a sample code with complete notes in this link for getting CPU and HD Drive ID: http://www.vcskicks.com/hardware_id.php

add this dll to refrences

 System.Management.dll

for CPU ID:

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

foreach (ManagementObject mo in moc)
{
     if (cpuInfo == "")
     {
          //Get only the first CPU's ID
          cpuInfo = mo.Properties["processorID"].Value.ToString();
          break;
     }
}
return cpuInfo;

and for Hard Drive ID (Volume Serial):

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.