Unique Hardware ID in C#
Any computer has components that carry a specific serial number. Some numbers are assigned when the operating system is installed, but some are actually embedded in the hardware. Using C#, multiple embedded serials can be combined to create a unique computer ID.
Why a unique serial? A simple reason is for branding software to a specific computer. A perhaps inflexible licensing system, it can be quite effective depending on how it is employed.
For this article we will use two serials, the hard drive and the CPU. Both will be obtained with the built-in C# class System.Management.
If you create a New Project in Visual Studio 2005 (any of the editions), you will notice the line “using System.Management” does not work. You have to add it manually:
- Go to the Solution Explorer
- Right-click on References and click “Add Reference…”
- Find System.Management from the .Net tab.
Once set up it is easy to get the hardware ID’s.
The hard drive ID’s depends obviously on the harddrive. Here is the pseudocode:
- Create a ManagementObject with the string “win32_logicaldisk.deviceid=[drive letter here]:”
- Access the serial with the index “VolumeSerialNumber”, for example disk[“VolumeSerialNumber”] where disk is the ManagementObject
The CPU ID is actually rather flexible also. Many computers nowadays have more than one CPU. You use the first one as in my example, or multiple ones:
- Create a ManagementObject with the string “win32_processor”
- Go through the available processors
- Access the property like so: managObject.Properties[“processorID”]
Combining them can be simple or complex. Simply adding them together in a row works just fine. For my example some redundant 0’s are removed before coming the ID’s of the first available drive and the first CPU.
Try it out to see how the numbers look.