C# Programming > Miscellaneous

C# Generate Unique Hardware ID

c# unique hardware ID

How to get the Computer's Unique ID

Each computer has certain hardware pieces that carry serial numbers. Using these serial numbers it is possible to obtain a hardware ID in C# that is unique to a certain computer only.

Keep in mind there are some serial numbers that are not true hardware serials, as in they change when the harddrive is formatted.

We are going to use the first CPU's ID and the first Harddrive's actual serial, neither of which change upon formatting. Using C# code, you can combine those serials into a unique hardware ID.

Hardware IDs

Both ID's will require the System.Management.dll reference. If you create a New Project the reference will not be added automatically.

Add it manually by right-clicking on "References" (in the Solution Explorer) and hit "Add Reference...". Scroll down to System.Management (in the .Net tab) and add it.


Note that finding the CPU ID is a bit different than normal C# programming. We'll be accessing the property through string indices rather than properties.

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();
return cpuInfo;

The function runs with a bit of a delay, but nothing too terrible. Try it once to see what the CPU ID looks like.

Hard Drive ID (Volume Serial)

Finding a unique volume serial works very similarly and luckily a bit simplier:

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

Deriving a Unique ID

The two serials can be combined in any way you wish. Use simple C# string functions or complex one. For example, after testing it across several systems, I decided to truncated serveral 0's from the Volume Serial.

A quick note. What is the point of a unique hardware ID? The most common use for such information is for licensing software that is tailored to a specific computer of set of computers. With a bit of C# encryption functions, it can be an effective method.

As a reader pointed out, the code will also works for removable media such as USB drives, which can make for some interesting scenarios.

Download the C# project files to see for yourself the speed of the combined functions and the complexity of the hardware ID.

Back to C# Article List