How to Get External Drives and Peripherals Using WMI Query

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

Background

Beside 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 get the background details.

UI Setup

WMI1.jpg

  • 2 Group Boxes with the "Select drive" and "Information" section.
  • 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:

WMI2.jpg

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

WMI3.jpg

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

Step 2

Now add a using 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:

  • Define a ManagementobjectSearcher object and the WMI query as an argument to its object as in the following:

ManagementObjectSearcher mangObjSearch = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

Here, we have named mangObjSearch as a refrence variable and "SELECT * FROM Win32_Disk Drive" is the WMI Query.

  • Next, we will add the active Drives present. So, we execute the foreach loop to get each individual drive present in 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 wil 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 specific 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 foreach loop to get the details and assign 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.

WMI5.jpg

Download Links

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

Up Next
    Ebook Download
    View all
    Learn
    View all