使用System.IO.DriveInfo类,很容易检测可插拔(Removable)设备信息,其中包括USB设备(U盘)。
using System.IO;
// 获取所有可插拔设备信息
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo di in drives)
{
// 判断设备是否就绪
if (di.IsReady && di.DriveType == DriveType.Removable)
{
Console.WriteLine("{0}已就绪", di.Name);
}
}
就是截获消息的方式知道插入和拔出