9

what kind of 'unique' system identifiers can be easily obtained using C# (to hash and then uniquely identify a system)? I could just hash HDD size and things like that but I need to identify and distinguish computers that are all built by the same components so I can't go by that.

Appreciate hints, ideas, sample code!

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
Alex
  • 75,813
  • 86
  • 255
  • 348
  • 1
    I'd be careful with this, that you don't make your customers hate you. They should be able to change network cards, hard disks, and memory size without telling you about it. – John Saunders Aug 21 '09 at 00:39
  • 4
    Oh, this requirement does actually come *from* the customer :) – Alex Aug 21 '09 at 01:40

6 Answers6

10

Here's a good start with WMI ...

// Win32_CPU will work too
var search = new ManagementObjectSearcher( "SELECT * FROM Win32_baseboard" );
var mobos = search.Get();

foreach (var m in mobos)
{
  var serial = m["SerialNumber"]; // ProcessorID if you use Win32_CPU
}

You can do that with many pieces of hardware and come up with a solution.

JP Alioto
  • 44,864
  • 6
  • 88
  • 112
  • I tried that ... but I see that these are days none of the serial nos are being maintained. Mother Board, Processor ... MAC is being changed by changing NIC ... what to choose? – Rick2047 Aug 25 '10 at 14:58
  • As the Win32_BaseBoard SerialNumber doesn't return a value in most cases anymore, your best bet is ProcessorID in Win32_Processor – Chris Watts Mar 22 '12 at 16:49
  • Note that ProcessorID is equal for all processors of the same type. If you test two i7-FooBar processors, both will have the same ID. – ANeves Mar 05 '18 at 18:40
8

You can use the System.Management namespace to get all stuff related to the hardware, ProcessorId, MAC addresses, and a lot more info you can then hash together.

Take a look at this article:

http://www.codeproject.com/KB/system/GetHardwareInformation.aspx

Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
  • To expand on the this and other answers a little bit: The MAC address of the network card(s) is going to be unique, even if the machines all use the same components. I would start with that. Second of all, you should be able to get motherboard and CPU serial numbers, as JP said. Those, plus the MAC address of physical NICs, should be easily enough to uniquely identify a machine. Just be aware that all of those can change if parts are replaced. – Jamie Penney Aug 20 '09 at 22:01
  • The MAC address of a network card can be set by software, so you cannot count on it being unique. On the other hand, I think main disk + mac address + processorId + motherboard could be a safe bet. – Vinko Vrsalovic Aug 20 '09 at 22:37
6

Presuming you are talking about Windows, then each Windows installation has a unique product id (which you can see when you view the properties of My Computer). I think this is stored in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion:ProductId(REG_SZ). I take it you want more than that?

Doug Schmidt
  • 207
  • 2
  • 13
Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
  • 1
    What about when the Windows OS image is copied into another computer? Will this registry key be the same on the clone machine? – Ulysses Alves Mar 15 '21 at 20:53
2

You can use WMI with C# to get quite a bit of information about hardware.

Hard Disk IDs and Ethernet MAC Address are two common unique identifiers used in system identification schemes. The MAC Address is, in theory, unique per network card.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    In theory, but in practice there is no single unique id that could be used, given that there are tons and easily accessible, it's best to grab two or three and combine them to create a unique string (or hash) – Vinko Vrsalovic Aug 20 '09 at 21:38
  • Yes, I agree. That's why the "in theory" :) – Reed Copsey Aug 20 '09 at 21:48
2

You could look into using GUID's

What is a GUID

For those of you who don't know, a GUID (pronounced goo'id - Globally unique identifier) is a 128-bit integer that can be used to uniquely identify something. You may store users or products in your database and you want somehow uniquely identify each row in the database. A common approach is to create a autoincrementing integer, another way would be to create a GUID for your products.

How to create a GUID in C#

The GUID method can be found in the System namespace. The GUID method System.Guid.NewGuid() initializes a new instance of the GUID class.

There are also a few overloads available for those of you who want the GUID formatted in a particular fashion.

The following live sample will output the GUID generated, the source code is also below.

Response.Write(@"<br>System.Guid.NewGuid().ToString() = " + System.Guid.NewGuid().ToString());
Response.Write(@"<br>System.Guid.NewGuid().ToString(""N"") = " + System.Guid.NewGuid().ToString("N"));
Response.Write(@"<br>System.Guid.NewGuid().ToString(""D"") = " + System.Guid.NewGuid().ToString("D"));
Response.Write(@"<br>System.Guid.NewGuid().ToString(""B"") = " + System.Guid.NewGuid().ToString("B"));
Response.Write(@"<br>System.Guid.NewGuid().ToString(""P"") = " + System.Guid.NewGuid().ToString("P"));
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
2

Network card MAC address:

foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
    // probably need to do some filtering on ni.NetworkInterfaceType here
    Console.WriteLine(ni.GetPhysicalAddress());
}
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • 1
    while this is a good idea, i thingk it would fail if the system does not have NIC! – Smith Oct 15 '11 at 15:26
  • 2
    This is not a good idea, Because This id changes when user disables a connection or if a software installs a virtual connection – a.toraby Jul 10 '15 at 10:40