How to detect removeable drive insertion

S

Steve Marshall

Hi All,

Apologies if this has come up before, but how can I set up something
that will notify me when a removeable drive (like a USB drive or
CompactFlash card) is inserted into its slot? Ideally I would like
some sort of event that would call my code when it happens. The
FileSystemWatcher object doesn't seem able to do this, unless I am
missing something.

Thanks
 
G

gene kelley

Hi All,

Apologies if this has come up before, but how can I set up something
that will notify me when a removeable drive (like a USB drive or
CompactFlash card) is inserted into its slot? Ideally I would like
some sort of event that would call my code when it happens. The
FileSystemWatcher object doesn't seem able to do this, unless I am
missing something.

Thanks

AFAIK, the easiest way to detect removable drive (and CD drive) media
insertion is to check the "IsReady" value of the drive.

What I do: At startup, I loop through the "DriveInfo.GetDrives()"
return and look for "DriveType.Removable" (excluding drive 0 which is
assumed to be a floppy drive if not a "DriveType.Fixed"). If a
removable drive is found, I check the "IsReady" value. I use a
GenericDictionary(Of Integer, Boolean) to which the removable drive
index and IsReady values are added. Start a timer.

The timer loops through the 3 or 4 items in the dictionary which hold
the removable drive indicies(keys) and IsReady values (Items). For
each key, the IsReady value is checked and compared to the existing
item value for that key. If the item value has changed, item value is
updated. If the change is from False to True, media was inserted - do
some other work. If the change was from True to False, media was
removed - probably ignore.

Gene
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top