How To Get The Serial Number Of Hard Drive By C#

To get the serial number of the hard disk, we need to use some C# classes.

ManagementObjectSearcher Class  is the class which initializes the new instance of ManagementObjectSearcher class. Basically, it is used to retrieve the management system of the system.
 
Let's Start.
 
Step 1: Creat a Project =>Right click on references, then Add System.Management, System.Management.Instrument.

Add a button and three labels to it.

 
Step 2 : Add the Namespaces to the project.

C# Code
  1. using System.Management;  
  2. using System.IO;  
  3. using System.Collections;  
Step 3: Declare an ArrayList.
  1. ArrayList hardDriveDetails = new ArrayList();  
Step 4: In the buttonClick event, write the following code. 
  1. private void button1_Click(object sender, EventArgs e)  
  2.        {  
  3.            ManagementObjectSearcher moSearcher = new  
  4.   ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");  
  5.   
  6.            foreach (ManagementObject wmi_HD in moSearcher.Get())  
  7.            {  
  8.                HardDrive hd = new HardDrive();  // User Defined Class
  9.                hd.Model = wmi_HD["Model"].ToString();  //Model Number
  10.                hd.Type = wmi_HD["InterfaceType"].ToString();  //Interface Type
  11.                hd.SerialNo= wmi_HD["SerialNumber"].ToString();  Serial Number
  12.                hardDriveDetails.Add(hd);  
  13.                label1.Text ="Model : "+ hd.Model ;
  14.                label2.Text = " Type : " + hd.Type;
  15.                label3.Text = " Serial Number : " + hd.SerialNo;
  16.            }  
  17.   
  18.        }  
Step 5: Add a user-defined class HardDrive having the following properties.
  1. class HardDrive  
  2.     {  
  3.         private string model = null;  
  4.         private string type = null;  
  5.         private string serialNo = null;  
  6.         public string Model  
  7.         {  
  8.             get { return model; }  
  9.             set { model = value; }  
  10.         }  
  11.         public string Type  
  12.         {  
  13.             get { return type; }  
  14.             set { type = value; }  
  15.         }  
  16.         public string SerialNo  
  17.         {  
  18.             get { return serialNo; }  
  19.             set { serialNo = value; }  
  20.         }  
  21.     }  
Step 6: Now, run the code.