I am using Environment.GetLogicalDrives(); to get a list of drives. I remember in c++ i could use GetDriveType to find if the device was CD, removable, flash, etc and i am t
DriveInfo is the class you are looking for.
You can use the DriveInfo type to retrieve a list of the drives. You need to check the DriveType property (enum)
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 the 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 the @TheCodeKing mentioned you can also use WMI to query drive information.
For example, you can query for USB sticks in the following manner:
ManagementObjectCollection drives = new ManagementObjectSearcher(
"SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'"
).Get();
Add a reference to the System.Management assembly if you are going to use WMI.
If you want to fill a 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();
}
To recapitulate:
You have use WMI for this, check this link for for information and examples.
Yes, the framework includes a DriveType enumeration used by the DriveInfo class. Have a look at the GetDrives() method on MSDN.