Many a times it is required that we find the hardware id of a system. The hardware can be Motherboard, processor or hard-disk. The manufactures of these devices embed a unique id to them which can be retrieved anytime. Further they are sometimes also used to get the unique id for the machine. In this post I am going to show how to retrieve these id's in C#.net code. First of all we would be requiring the following namespace:
System.Management
The following code will retrieve the CPU ID (processor id):
ManagementObjectCollection mbsList = null;
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_processor");
mbsList = mbs.Get();
string id="";
foreach (ManagementObject mo in mbsList)
{
id = mo["ProcessorID"].ToString();
}
The following code will generate hard-disk id:
ManagementObject dsk = new ManagementObject(@"win32_logicaldisk.deviceid=""c:""");
dsk.Get();
string id = dsk["VolumeSerialNumber"].ToString();
The following code will generate motherboard id:
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
ManagementObjectCollection moc = mos.Get();
string serial="";
foreach (ManagementObject mo in moc)
{
serial = (string)mo["SerialNumber"];
}
By using these we can generate the id's of the respective hardware. Another use of these can be to generate a unique id for that system. But one should be aware that these id's are not always unique. It has been seen that some hardware manufactures use a single id for a whole lot of hardware. In this scenario we can use combination of all these id's to generate a single unique id for the machine.