Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect a USB drive has been plugged in?

Tags:

c#

file

usb-drive

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?

like image 878
angel Avatar asked May 14 '11 18:05

angel


People also ask

How do you tell if a flash drive has been used?

Depending on how many files you have on the USB, what you can do is right-click on the files and click properties. There it should give you information about the file such as when it was created, last modified, and last accessed.

Can you tell what computers your flash drive has been plugged into?

Removable devices including USB sticks don't store this information. You can perhaps find some specific products which implement an auditing / logging technology to remember the number / type of devices it has been plugged in, but if your specific device don't implement it then you can't.

Can a USB drive be tracked?

Yes, it can be tracked. Its more common to just prevent users from using USB drives.


1 Answers

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:

  • The DriveInfo class (msdn documentation)
  • The DriveType enumeration (msdn documentation)
like image 139
Elian Ebbing Avatar answered Sep 23 '22 06:09

Elian Ebbing