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
 
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 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 References".
 
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 reference 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 on the computer.
  1. foreach(ManagementObject mangObj in mangObjSearch.Get())  
  2. {  
  3.     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.
  1. 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:
  1. 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 them to respective labels.
  1. foreach(ManagementObject manObj in mangObjsearc.Get())  
  2. {  
  3.     typeLable.Content = manObj["MediaType"].ToString();  
  4.     modelType.Content=manObj["Model"].ToString();  
  5.     serialType.Content=manObj["SerialNumber"].ToString();  
  6.     interfaceLable.Content=manObj["InterfaceType"].ToString();  
  7.     capacityLable.Content=Math.Round((((Convert.ToDouble(manObj["Size"])/1024)/1024)/1024))+" GB";  
  8.     partisionLable.Content=manObj["Partitions"].ToString();  
  9.     signatureLable.Content=manObj["Signature"].ToString();  
  10.     firmwareLable.Content=manObj["FirmwareRevision"].ToString();  
  11.     cylinderLable.Content = manObj["TotalCylinders"].ToString()+" Cylinders";  
  12.     sectorsLable.Content = manObj["TotalSectors"].ToString()+" Sectors";  
  13.     headsLable.Content = manObj["TotalHeads"].ToString()+" Heads";  
  14.     trackLable.Content = manObj["TotalTracks"].ToString()+" Tracks";  
  15. }
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


Similar Articles