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
- using System.Management;
- using System.IO;
- using System.Collections;
Step 3: Declare an ArrayList.
- ArrayList hardDriveDetails = new ArrayList();
Step 4: In the buttonClick event, write the following code.
- private void button1_Click(object sender, EventArgs e)
- {
- ManagementObjectSearcher moSearcher = new
- ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
-
- foreach (ManagementObject wmi_HD in moSearcher.Get())
- {
- HardDrive hd = new HardDrive(); // User Defined Class
- hd.Model = wmi_HD["Model"].ToString(); //Model Number
- hd.Type = wmi_HD["InterfaceType"].ToString(); //Interface Type
- hd.SerialNo= wmi_HD["SerialNumber"].ToString(); Serial Number
- hardDriveDetails.Add(hd);
- label1.Text ="Model : "+ hd.Model ;
- label2.Text = " Type : " + hd.Type;
- label3.Text = " Serial Number : " + hd.SerialNo;
- }
-
- }
Step 5: Add a user-defined class HardDrive having the following properties.
- class HardDrive
- {
- private string model = null;
- private string type = null;
- private string serialNo = null;
- public string Model
- {
- get { return model; }
- set { model = value; }
- }
- public string Type
- {
- get { return type; }
- set { type = value; }
- }
- public string SerialNo
- {
- get { return serialNo; }
- set { serialNo = value; }
- }
- }
Step 6: Now, run the code.