如何检测USB驱动器已插入? [英] How to detect a USB drive has been plugged in?
问题描述
我想构建一个程序来检测是否插入了一个USBb
(并将所有内容复制到硬盘上的任何文件夹)。
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)
任何想法?我有这个,
using System.Runtime.InteropServices;
但这不是简单的方法(我相信)。我想要一些容易的东西
But it is not the easy way (that I believe). I want something easy.
我有另一个想法(if(文件夹存在)然后复制)某些东西 - 但可能有问题,我想要一个很好的解决方案。
I have another idea (if (folder exist) then copy) something -- but there may be a problem with that, and I want a good solution.
也可能有一个名为SerialPort的工具;我可以用吗如果是这样,我该如何使用?
There may also be a tool called SerialPort; can I use it? If so, how do I use it?
推荐答案
容易检查可移动设备,但不能保证它是一个USB设备:
It is easy to check for removable devices, however it doesn't guaranty that it is an 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:
-
DriveInfo
class (msdn文档) -
DriveType
枚举 (msdn文档)
- The
DriveInfo
class (msdn documentation) - The
DriveType
enumeration (msdn documentation)
这篇关于如何检测USB驱动器已插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!