Detecting memory card/USB device insertion - how to?

  • Thread starter Thread starter Chris Lewis
  • Start date Start date
C

Chris Lewis

Hi,

I want my app to respond to attachment of a memory card (SD) and/or a
particular type of USB device to a client PC. Both devices have on them the
same file/directory structure - i.e. a directory structure recognisable to
my application.

What I would like to do is have my .NET Windows Form app start automatically
(if it is not already running) when either device is attached to the
machine, and have the files pre-selected in an explorer-like window for the
user to take further action with. Adobe Elements does this, for example,
when I attach my Compact Flash card to my PC - it seems to use a Windows
Service called 'Adobe Active File Monitor' that runs constantly in the
background.

Any ideas how to do this, or better still any code out there that already
does this? I don't think the .NET FileSystemWatcher will work for removable
devices, so I suspect that a bespoke polling component is needed. If so, how
is it best to detect the presence or otherwise of removable drives?

Thanks,

Chris
 
Thanks for the link. I have tried running this on my XP SP2 machine, and
nothing seemed to happen (i.e. no events were fired at all) when I attached
a CompactFlash card via a multi-card USB reader? Any ideas?

Thanks,

Chris
 
Try what one of the comments on that site suggests for that article:

"The reason why Riul's solution doesn't work for anything except CD/DVD
drives is the line
if(lBroadcastHeader.Type==DeviceType.Volume)
in _DeviceVolumeMonitor.WndProc(), file DeviceVolumeMonitor.cs line 108.

Remove/comment the if and always trigger the events to also get
notifications for USB or Network devices."

Chris
 
Thanks for this Chris.

I went back to the code with your suggestion, but found that commenting the
line you mention made no difference. However, I commented out line 111
instead ('if((lVolume.Flags & (int)VolumeFlags.Media)!=0)') and this did
work, but only for a removable device (an SD card) plugged directly into the
SD slot on my laptop. Devices plugged into an attached USB multi-card reader
were not picked up.

I have achieved what I need to do using the following article buried in the
MSDN help for VS2003 - article called 'Drives and Directories and Files! Oh
my!' (MSDN URL
'ms-help://MS.VSCC.2003/MS.MSDNQTR.2005OCT.1033/dnadvnet/html/vbnet07232002.htm')
which demos use of the Windows Scripting Host COM object to obtain device
and directory info from all connected devices - I have written some code
based on the article to poll the connected drives periodically on a
dedicated thread and raise events when drives are inserted or removed, and
it works exactly as I need it to - including using the USB card reader.

Chris
 
Back
Top