Pages

Social Icons

Monday 14 November 2011

Getting Hard Disk Serial No. in C#


Every hard disk has a unique serial number which we can get by our code here is a example which gives you the you computer's c drive serial number.

using System.Management;

public string GetHDDSerialNumber(string drive)
        {
            //check to see if the user provided a drive letter
            //if not default it to "C"
            if (drive == "" || drive == null)
            {
                drive = "C";
            }
            //create our ManagementObject, passing it the drive letter to the
            //DevideID using WQL
            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + drive + ":\"");
            //bind our management object
            disk.Get();
            //return the serial number
            return disk["VolumeSerialNumber"].ToString();
        }

No comments:

Post a Comment