Search code examples
c#dvdusb-flash-drivecd-romusb-drive

Detecting if disk is flash drive


I wanna write program that detects flash drives.
But there's a problem.
Code:

DriveInfo[] allDrives = DriveInfo.GetDrives();

foreach (DriveInfo drive in DriveInfo.GetDrives())
{
    if (drive.DriveType == DriveType.Removable)
    {
    }
}

It works well, but it detects cdrom too. How to prevent it?


Solution

  • I have no answer why your code is not working. But if you want to detect USB-devices, you could also try it with the WMI like this:

    ManagementObjectCollection drives = new ManagementObjectSearcher(
        "SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'"
    ).Get();
    

    Add the System.Management assembly to your project to do it like this.