How to Get External Drives and Peripherals Using WMI Query

We will develop an application showing your hard disk background details or any external drives/peripherals using a WMI Query.

Background

Besides the fact, we can't do it solely in C# code to retrieve the background details of the hard disk or any external drives.

So we need a WMI Query to execute and get the background details.

UI Setup

UI Setup

  • 2 Group Boxes with the "Select drive" and "Information" sections.
  • An image (you'll get it from http://iconfinder.com)
  • A Combo Box, that will have your present Drive list.
  • 12 Labels, that will show respective details.

Note. Here, we have used a Grid Control that has 12 rows and 2 columns to get the correct alignment.

I suppose you designed the same-looking User Interface. Now, we will move to the code section.

Backend C# Code

Step 1. To get the WMI Query, we need the System. Management namespace.

In your Solution Explorer.

Solution explorer

Right-click on references, and select "Add References".

Add References

In the .NET tab, select "System. Management".

Step 2. Now add a use for "System. Management" in your C# code.

Double-click on your form to get the Form_Load() event or Window_Load() event.

Under that event

  1. Define a ManagementobjectSearcher object and the WMI query as an argument to its object as in the following.
  2. ManagementObjectSearcher mangObjSearch = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
  3. Here, we have named mangObjSearch as a reference variable, and "SELECT * FROM Win32_Disk Drive" is the WMI Query.
  4. Next, we will add the active Drives present. So, we execute the foreach loop to get each individual drive present on the computer.
    foreach (ManagementObject mangObj in mangObjSearch.Get())
    {
        hardDiskComboBox.Items.Add(mangObj["Model"].ToString());
    }

Since we know that mangObjSearch is of ManagenementObjectType, we have typed the loop counter (in other words mangObj) of the ManagementObject.

Inside the loop, we added to the Combo Box (i.e. hardDiskComboBox), so we have the array of mangObj to be added to the combo Box.

That much we have in the Window_Load() event.

Step 3. When we select the Combo Box item then it will raise the Change event.

We will now code that specific event, the SelectionChanged() event.

Again, we will define a ManagenmentObjectSearcher object.

ManagementObjectSearcher mangObjsearc = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Model='" + hardDiskComboBox.SelectedItem + "'");

Here, we have a different WMI query, since now we want the background details of that specifically selected drive.

So,

SELECT * FROM Win32_DiskDrive WHERE Model = '<selected Item>';

Here, the "*" (asterisk) signifies all the details.

For the rest, we use the same for each loop to get the details and assign them to respective labels.

foreach(ManagementObject manObj in mangObjsearc.Get())   
{   
    typeLable.Content = manObj["MediaType"].ToString();   
    modelType.Content = manObj["Model"].ToString();   
    serialType.Content = manObj["SerialNumber"].ToString();   
    interfaceLable.Content = manObj["InterfaceType"].ToString();   
    capacityLable.Content = Math.Round((((Convert.ToDouble(manObj["Size"]) / 1024) / 1024) / 1024)) + " GB";   
    partisionLable.Content = manObj["Partitions"].ToString();   
    signatureLable.Content = manObj["Signature"].ToString();   
    firmwareLable.Content = manObj["FirmwareRevision"].ToString();   
    cylinderLable.Content = manObj["TotalCylinders"].ToString() + " Cylinders";   
    sectorsLable.Content = manObj["TotalSectors"].ToString() + " Sectors";   
    headsLable.Content = manObj["TotalHeads"].ToString() + " Heads";   
    trackLable.Content = manObj["TotalTracks"].ToString() + " Tracks";   
}

Note. Follow the correct format of the attributes.

Ultimately, you will get your application.

Application

Download Links

Solution File: https://www.dropbox.com/s/nujijfi1mjqfb3a/Disk%20Manager.rar


Recommended Free Ebook
Similar Articles