2

I have to write a function that returns all drive letters where the drive is a USB stick. The problem is that the USB sticks might be partitioned in way that Windows assigns 2 or more Drives to a single USB stick of which only one is a USB mass storage device(that is the letter that I want). So stuff like DriveInfo.GetDrives() and then filtering with device.DriveType == DriveType.Removable does not work because that returns all the drives that are removable.

I have been trying to do it in WMI but so far I have not found the right answer. The most common answer to similiar questions is to go throug a few classes like this (source): Win32_DiskDrive-> Win32_DiskDriveToDiskPartition -> Win32_DiskPartition -> Win32_LogicalDiskToPartition -> Win32_LogicalDisk But that also returns all drives of the USB sticks. My thinking would be to remove all the drives that are not mass storage devices with Win32_USBHub. But I don't know where to add it in the chain or if that is even possible? Any help?

jontte
  • 41
  • 2
  • You only want the drives that "IsReady == true". See : https://stackoverflow.com/questions/37645456/driveinfo-getdrives-show-not-connected-network-drives?force_isolation=true – jdweng May 25 '21 at 12:46
  • No that does not change anything, both drives are ready – jontte May 25 '21 at 13:00
  • What happens if you use a file explorer to view the directories? If the file explorer is seeing both drives then the are mounted to the machine and are valid. – jdweng May 25 '21 at 13:02
  • Yes I can see both with the explorer, but I only want the drive that has the USB logo in the explorer – jontte May 25 '21 at 13:04
  • You could get list of USB devices and check which ones are mounted as drivers (not serial) : https://learn.microsoft.com/en-us/uwp/api/windows.devices.usb.usbdevice?force_isolation=true&view=winrt-19041 – jdweng May 25 '21 at 13:20
  • I am having trouble adding the Windows.Devices.Usb.dll to my project. I am getting this error message: "...Usb.dll could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component". Tried to register it with "regsvr32", but that also doesn't work – jontte May 25 '21 at 14:46
  • You need to use ImportDll. You are using a unmanaged library which cannot be called from c# unless you use ImportDll. – jdweng May 25 '21 at 14:52

0 Answers0