Unique Hardware ID in C#

June 7, 2022 0 Comments

Any computer has components that carry a specific serial number. Some numbers are assigned when the operating system is installed, but some are built into the hardware. With C#, multiple embedded serials can be combined to create a single computer ID.

Why a unique series? A simple reason is brand software for a specific computer. A perhaps inflexible licensing system can be quite effective depending on how it is used.

For this article we will use two serials, the hard drive and the CPU. Both will be obtained with the built-in C# System.Management class.

If you create a new project in Visual Studio 2005 (either edition), you’ll notice that the “using System.Management” line doesn’t work. You have to add it manually:

  • Go to Solution Explorer
  • Right click on References and click “Add Reference…”
  • Look for System.Management in the .Net tab.

Once configured, it’s easy to get the hardware ID.

The hard drive ID obviously depends on the hard drive. 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 also quite flexible. Many computers today have more than one CPU. You use the first one like in my example, or several:

  • 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 in a row works fine. For my example, some redundant 0’s are removed before the IDs of the first available unit and the first CPU appear.

Give it a try to see what the numbers look like.

Leave a Reply

Your email address will not be published. Required fields are marked *