You can use the DriveInfo type to get a list of drives. You need to check the DriveType (enum) property
var drives = DriveInfo.GetDrives(); foreach (var drive in drives) { if (drive.DriveType == DriveType.Removable) { Console.WriteLine(drive.Name); } }
You can also use LINQ-to-Objects to query drives:
var drives = from drive in DriveInfo.GetDrives() where drive.DriveType == DriveType.Removable select drive; foreach(var drive in drives) { Console.WriteLine(drive.Name); }
Like @TheCodeKing mentioned, you can also use WMI to request disk information.
For example, you can request USB drives as follows:
ManagementObjectCollection drives = new ManagementObjectSearcher( "SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'" ).Get();
Add a reference to the System.Management assembly if you intend to use WMI.
If you want to populate the ComboBox in a Windows Forms application with this data, you need to bind the results to the ComboBox control.
For example:
private void Form1_Load(object sender, EventArgs e) { var drives = from drive in DriveInfo.GetDrives() where drive.DriveType == DriveType.Removable select drive; comboBox1.DataSource = drives.ToList(); }
Repeat:
- Add a ComboBox control to a Windows form (drag it to the form from the toolbar)
- Request removable drives.
- Bind results to ComboBox.
Christophe geers
source share