Detecting and getting files from USB storage devices

Hello, for a project I'm working on, I want to be able to detect when a USB storage device is inserted, and do something with the files on it once the USB device has been detected.
I'll be on Windows and I've tried using the rusb crate, a wrapper for libusb, which allows me to list all USB devices. The "hotplug" feature that allows one to detect when a USB device has been inserted is not supported, although you can still poll for devices periodically and detect any changes instead (which may not be as elegant, but I don't care much about that in this project). My question is, are there any better alternatives to this approach? And more importantly, how does one start using the files that are on the USB storage device? Forgive me as I do not have much knowledge on USB devices and how you're able to differ from things like a mouse or keyboard vs a storage device. Any help is greatly appreciated!

I don't know how exactly it works on Windows, but you will almost certainly want to detect new drives, not new USB devices. Then you will know what drive letter (Windows) or mount point (Unix) you should access the files through, since the detection API will tell you that.

1 Like

I think GetDriveType() and GetLogicalDrives() in crate winapi is useful. Since you are on Windows, using Windows API can be convenient but unsafe.
Or else, just a silly idea,on std::path you may check letters from A to Z through exists(). You can infer that a USB storage device is inserted when existing letters grow more.Methods for Path and PathBuf may be of use.
My computer is absent, So only attempted solutions I can provide.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.