11 December 2008

C# Read Harddisk Serial Number WMI ManagementClass Win32_LogicalDisk

With the Win32_LogicalDisk Management path we can reach the local disk information.

Even you can read harddisk serial number,
size, partitions, free space and so on.

Now let's read the serial number of a harddisk in C# with ManagementClass WMI.
public static string HardDiskID()
ManagementClass partionsClass = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection partions = partionsClass.GetInstances();

string hdd = string.Empty;

foreach (ManagementObject partion in partions)
hdd = Convert.ToString(partion["VolumeSerialNumber"]);

if (hdd != string.Empty)
return hdd;

return hdd;
This static function will simple give us the volume serial numbers.
C# Read Harddisk Serial Number WMI ManagementClass.


Mantis-89 said...

Interesting, but this read the sn of the partition, not of the hd.

MARUF said...

Good essay!

I tried to learn hard disk number but when I compare them I take COM error, but your code is working properly! (TURKEY)