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.
//
// Deriving a Unique ID from the CPU ID and Volume 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.
//
private string getUniqueID(string drive)
{
if (drive == string.Empty)
{
//Find first drive
foreach (DriveInfo compDrive in DriveInfo.GetDrives())
{
if (compDrive.IsReady)
{
drive = compDrive.RootDirectory.ToString();
break;
}
}
}
if (drive.EndsWith(":\\"))
{
//C:\ -> C
drive = drive.Substring(0, drive.Length - 2);
}
string volumeSerial = getVolumeSerial(drive);
string cpuID = getCPUID();
//Mix them up and remove some useless 0's
return cpuID.Substring(13) + cpuID.Substring(1, 4) + volumeSerial + cpuID.Substring(4, 4);
}
private string getVolumeSerial(string drive)
{
ManagementObject disk = new ManagementObject(@"win32_logicaldisk.deviceid=""" + drive + @":""");
disk.Get();
string volumeSerial = disk["VolumeSerialNumber"].ToString();
disk.Dispose();
return volumeSerial;
}
private string getCPUID()
{
string cpuInfo = "";
ManagementClass managClass = new ManagementClass("win32_processor");
ManagementObjectCollection managCollec = managClass.GetInstances();
foreach (ManagementObject managObj in managCollec)
{
if (cpuInfo == "")
{
//Get only the first CPU's ID
cpuInfo = managObj.Properties["processorID"].Value.ToString();
break;
}
}
return cpuInfo;
}