How to detect a USB drive has been plugged in?

angel picture angel · May 14, 2011 · Viewed 102.5k times · Source

I want to build a program that detects if a usb (or two or more) are plugged in (and copy all contents to any folder on a hard disk)

Any ideas? I have this,

using System.Runtime.InteropServices;

But it is not the easy way (that I believe). I want something easy.

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

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

Answer

Elian Ebbing picture Elian Ebbing · May 14, 2011

It is easy to check for removable devices. However, there's no guarantee that it is a USB device:

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

This will return a list of all removable devices that are currently accessible. More information: