How to detect a USB drive connected?

I want to create a program that detects if usb (or two or more) are connected (and copy all the contents to any folder on the hard drive)

Any ideas? I have it

using System.Runtime.InteropServices; 

But this is not an easy way (I think). I want something easy.

I have another idea (if (the folder exists), then copy) something, but there may be a problem with this, and I want a good solution.

There may also be a tool called SerialPort; can i use it? If so, how to use it?

+47
c # file usb-drive
May 14 '11 at 18:05
source share
4 answers

It is easy to check for removable devices, however this does not guarantee that this is a USB device:

 var drives = DriveInfo.GetDrives() .Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable); 

This will return a list of all available removable devices. Additional Information:

+53
May 14 '11 at 18:19
source share

Detecting if a USB drive is inserted is not easy. However, this is definitely possible, and work has been done for you. Here are two links, each of which contains an answer on how to do this in C #:

http://www.codeproject.com/KB/system/DriveDetector.aspx

http://www.codeproject.com/Messages/2126647/Re-Csharp-USB-Detection.aspx

Any solution should work just fine for you. You can adapt them to suit your needs.

Edit: Here is the third solution if you need more options (basically, they are all the same, but the choice is good, right?):

http://tempuzfugit.wordpress.com/2007/10/08/external-storage-unit-detection-with-c-in-net-usb-card-readers-etc/

+18
May 14 '11 at 18:11
source share

Here is the code that works for me, which is part of the site above combined with my early tests: http://www.codeproject.com/KB/system/DriveDetector.aspx

This basically makes your form listen for Windows messages, filters for USB drives and (cd-dvds), grabs the lparam structure of the message and extracts the drive letter.

 protected override void WndProc(ref Message m) { if (m.Msg == WM_DEVICECHANGE) { DEV_BROADCAST_VOLUME vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); if ((m.WParam.ToInt32() == DBT_DEVICEARRIVAL) && (vol.dbcv_devicetype == DBT_DEVTYPVOLUME) ) { MessageBox.Show(DriveMaskToLetter(vol.dbcv_unitmask).ToString()); } if ((m.WParam.ToInt32() == DBT_DEVICEREMOVALCOMPLETE) && (vol.dbcv_devicetype == DBT_DEVTYPVOLUME)) { MessageBox.Show("usb out"); } } base.WndProc(ref m); } [StructLayout(LayoutKind.Sequential)] //Same layout in mem public struct DEV_BROADCAST_VOLUME { public int dbcv_size; public int dbcv_devicetype; public int dbcv_reserved; public int dbcv_unitmask; } private static char DriveMaskToLetter(int mask) { char letter; string drives = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //1 = A, 2 = B, 3 = C int cnt = 0; int pom = mask / 2; while (pom != 0) // while there is any bit set in the mask shift it right { pom = pom / 2; cnt++; } if (cnt < drives.Length) letter = drives[cnt]; else letter = '?'; return letter; } 

Remember to add this:

 using System.Runtime.InteropServices; 

and the following constants:

  const int WM_DEVICECHANGE = 0x0219; //see msdn site const int DBT_DEVICEARRIVAL = 0x8000; const int DBT_DEVICEREMOVALCOMPLETE = 0x8004; const int DBT_DEVTYPVOLUME = 0x00000002; 
+10
Oct 17 '12 at 11:09
source share

Microsoft Code Code Pack. Class ShellObjectWatcher.

+2
Feb 19 '12 at 13:21
source share



All Articles