C#

Get USB information in C#

We will see how we can retrieve various information about usb devices in C#. We need the class ManagementObjectSearcher and Win32_DiskDrive class. To use this class we have to add a reference to System.Management namespace.

and now.. the interesting part:

1
2
3
4
5
6
7
8
9
10
ManagementObjectSearcher theSearcher = 
	new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
foreach (ManagementObject currentObject in theSearcher.Get())
{
	MessageBox.Show(
		"USB Caption: " + currentObject["Caption"].ToString() + "rn" +
		"USB Device ID: " + currentObject["DeviceID"].ToString() + "rn" +
		"USB size (GB): " + 
		Math.Round((((Convert.ToDouble(currentObject["Size"]) / 1024) / 1024) / 1024), 2));
}