Find USB drive letter from VID/PID (Needed for XP and higher)

前端 未结 3 1954
逝去的感伤
逝去的感伤 2021-02-01 21:21

So I thought I would include the final answer here so you don\'t have to make sense of this post. Big thanks to Simon Mourier for taking the time to figure this one out.

3条回答
  •  悲哀的现实
    2021-02-01 21:59

    I've had the same problem and also browsing through the WMI did not help me out, really.

    But I ended up with these few lines of code, and it works great for me:

    private string GetAvailableStorageDrive()
    {
        foreach (var info in System.IO.DriveInfo.GetDrives())
        {
            if (info.DriveType == System.IO.DriveType.Removable && 
                 info.IsReady && 
                !info.Name.Equals("A:\\"))
            {
                return info.Name;
            }
        }
        return string.Empty;
    }
    

    Basically the above function looks if the DriveType is Removable andalso if the Drive is ready. I also exclude the drive letter 'A' because in default windows environments, this is the Floppy.

    Description of DriveType.Removable:

    The drive is a removable storage device, such as a floppy disk drive or a USB flash drive.

    Note: As CodeCaster pointed out, this function will also return removable storage devices such as SATA. So if that's the case, you will have to look into more complex solutions as provided by others.

提交回复
热议问题