Detecting USB flashdrives

Hi all,

For an embedded application (both Linux and Windows), I need to find an update mechanism.
The host of the application will not be connected to the Internet (or any network for that matter).

Is there a way to detect if a USB flash drive has been inserted, preferably without using plugins?
What I would like to do is that the application would search for a specific file in a specific folder on a USB key (for example a .zip file), extract it to a temporary folder, and then update the program.

I can do the latter part, I just need a way to detect the USB flashdrive.
Unfortunately, Windows tends to change the driveletter for flashdrives. Not sure if Linux does this as well.

Any ideas?
Thank you in advance.

You prefer this scheme over an internet based solution?

Ug, I read your post too fast. Disregard my question.

I did a project recently that recognized USB drives so I could copy files to them. Unfortunately, it used the MBS plugins and the Mac version was quite a bit different than Windows. I didn’t try Linux so I can’t help you there. I think you might have to resort to a plugin.

use a Timer, every second or so, that count the mounted volumes with the “VolumeCount” method.
store the number at app startup
if the number changes, then someone has mounted another volume
then check for the content of that volume if you find the zip file you want.

I’d go one step further and include an invisible file at the top level of a known name that contains data about the file.

For example, the invisible file might be named “.myapp_update_info.txt” and would be JSON with RSA signature as the first line. You would check that signature against the RSA public key embedded in your app, then read the remainder into a JSONItem. That can contain information like the included version and a hash of the zip file. If the found zip matches the hash, and it’s of a newer version, you can proceed with the update.

Scanning every volume for that file should be trivial.

FS events
There may be code in MacOSLib for this

Except he wants Windows/Linux.

WFS has code for this

On a similar note - would the code below work (for OS X) - although I presume this would not necessarily be USB drive specific, as the user could also insert an SD card

// INSERT INTO THE APP OPEN EVENT Dim vc as Integer = VolumeCount

// INSERT INTO A TIMER For i = 0 to VolumeCount If i > vc Then // NEW VOLUME MOUNTED - SO DO WHAT NEEDS TO BE DONE Next

A assume you meant that vc would be an App property. And you wouldn’t need the loop, just compare VolumeCount to vc.

If your user manages to swap drives between the Timer periods, your app will miss it. On the other hand, if you continuously scan the drives, you will keep them awake and spinning, and that’s a no-no.

Thanks Kem.

So the code can be shortened as below:

// INSERT INTO THE APP OPEN EVENT Dim vc as Integer = VolumeCount


The only problem then would be - if you cannot check via a timer due to keeping the drive(s) spinning - how can you possibly check for insertion of an additional drive?

If you check too often - the drive is always awake.
If you check after a longer period - a drive could have been inserted between checks - defeating the object.

Do you recommend some other way of simply checking for an inserted drive - or is there no real way of doing this without the overhead (preventing drive sleep)?


Hence why I’d suggest using an event based solution

See WFS PlugAndPlay.WatchForVolumeDeviceEvents

Except that doesn’t help for Linux.

Looking at volumes can be done in a timer which triggers an event when a new volume is detected. That works on all three platforms.

But as Kem pointed out - checking via a timer means that the drive cannot powerdown, due to being continually checked.

open a shell, use command lsusb to check if there are more usb devices than were when app was started. Using lsusb with options -sa:b -v gives more info on usb device b found on bus a (the number, such as Mass Storage Device, Block Device etc.

It seems there are valid MBS plugins, MacOSLib and WFS methods for Mac and Windows. Reinventing the wheel with such resources makes no sense.

Since Linux has neither, the volume approach remains. The timer does not have to spin too fast, though. Something like every 5 or ten seconds could be civilized enough to let the user unmount the drive if needed. Eventually, the software could even be pause to unmount.

Surely though - whatever method is used to check for inserted USB devices - the fact remains that the check is performed at regular / semi regular periods - resulting in the prevention of the OS putting the hard drive to sleep?

In Windows, and probably in Linux as well, there are flags in the Registry which show how many devices have been inserted without polling them. I believe MacOSLib, WFS and MBS do the same. Maybe the method Thomas Rottensteiner posted does that.

There is a difference between devices and volumes. devices are detected electronically. Volumes require reading the drive.