Detect removable media

Hi all.

I am making some software that syncronizes files on a local hard drive with the files on an USB 2.0 flash drive. The drive is totally plug and play, so after I plug the drive into my USB port, it shows up in my computer as "Removable Disk (G:)"

My question is this: What can I do to
1) Detect when the drive is plugged in
2) Detect what drive letter it is

Thanks for any help, I will up the points if this is very complicated.
LVL 4
srcalcAsked:
Who is Participating?
 
ClifConnect With a Mentor Commented:
I just tested my USB flash drive with the sysinfo.ocx on VB.NET.  The 'DeviceArrival' event works for me.

    Private Sub AxSysInfo1_DeviceArrival(ByVal sender As Object, ByVal e As AxSysInfoLib.DSysInfoEvents_DeviceArrivalEvent) Handles AxSysInfo1.DeviceArrival
        Debug.WriteLine("DeviceArrival ")
        Debug.WriteLine(e.deviceData)
        Debug.WriteLine(e.deviceID)
        Debug.WriteLine(e.deviceName)
        Debug.WriteLine(e.deviceType)
    End Sub

Produces the following output:
DeviceArrival
0
64

2
0
 
srcalcAuthor Commented:
Perhaps I can use some sort of code with the FileSystemWatcher?
0
 
srcalcAuthor Commented:
I know I'm being impatient, but I'm raising the points for an answer.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
armoghanCommented:
0
 
armoghanCommented:
Try the last link
0
 
srcalcAuthor Commented:
OK I will see what I can do with those and get back to you
0
 
srcalcAuthor Commented:
I am having trouble trying to figure out what the code in that last link is doing. Can you identify the important parts?
0
 
srcalcAuthor Commented:
The 'DeviceArrival' event from sysinfo.ocx that the author talks about in http://www.experts-exchange.com/Storage/Q_20833060.html seems to work, but only for VB 6.0. Any ideas?
0
 
armoghanCommented:
have you tried the first link on your Removable Disks?. This seems simple
http://www.codeproject.com/dotnet/devicevolumemonitor.asp


>>Perhaps a VB .NET implimentation of this: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/removable_storage_manager_functions.asp

I could not find an implemnation for this :(
0
 
srcalcAuthor Commented:
I downloaded the file from codeproject.com
The program can detect when i put a CD in, but not when i put in a USB drive...
0
 
srcalcAuthor Commented:
Clif,
Where are you getting AxSysInfo?
0
 
ClifCommented:
I added the VB6 sysinfo ocx to the project, then dropped it on the form.  I double clicked it to get event code, then selected DeviceArrival from the event dropdown list.

What I posted is what VB.NET inserted.
0
 
srcalcAuthor Commented:
when i drop the sysinfo.ocx into my form, it says that i need a license
0
 
ClifCommented:
You will need to install VB6 on your development machine.
0
 
srcalcAuthor Commented:
Thanks for the fast feedback. Actually I just found a link that show me how to do it with my VS .NET CD:
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q318/5/97.asp&NoWebContent=1

Let me work with the control and make sure I have no other questions about it, then I will award points.
0
 
ClifCommented:
I did not know this.  Perhaps I should award you points?  :)
0
 
srcalcAuthor Commented:
Why does the DeviceArrival event not tell me the device name?

DeviceArrival      ===The event
0                       ===DeviceType - OEM-defined device type.
64                     ===DeviceID - G:\
                         ===Where is the name??
2                       ===DeviceData - OEM-specific function value. Possible values depend on the device.
0
 
ClifCommented:
Don't know.

Perhaps it's something internal to the USB flash drive.  It just doesn't contain a name.

Did you get yours to work?  

Do you have a USB mouse?  Try unplugging and replugging it in.  Does it display a name?
0
 
srcalcAuthor Commented:
The event only seems to trigger for devices that show up as a drive. EX:

My USB mouse didn't trigger it; niether did my old TI-83 graphlink
My USB drive did trigger it; my digital camera also did (It shows up as a drive letter)

I didn't get a name for my camera either
0
 
ClifCommented:
Oh well, I suppose you don't really need a name.  "64" should be the device ID to be on the look out for.  No, "64" doesn't mean "Drive G:", it should just mean "Flash Drive".  You might want to check it for other flash drives (including various manufacturers) just to be sure.

As far as telling which drive it's in, that's going to be a bit trickier.  "Removable Disk" could be a flash drive, or it could be a zip drive or ???.  It should take the first available letter after "C", but you'd have to know that the flash drive wasn't plugged in before your application started.

In any event, it's a little beyond my ability at this point.
0
 
srcalcAuthor Commented:
OK, thanks for you help. Actually, I think that 64 is the drive letter. 2^(5+1) = 64 and G is the 5th letter of the alphabet. A would be 1, B 2, C 4, D 8, E 16, F 32, G 64. At any rate, thanks for the code you pasted, it helped. Give me a little more time to experiment and I will award points.
0
 
srcalcAuthor Commented:
Clif,
If you post on my pointer link http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21133767.html within a day i will award you points. Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.