C#如何捕获USB设备(U盘等)插入与拔出电脑的消息?

2025-04-18 03:29:01
推荐回答(2个)
回答1:

使用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);
    }
}

回答2:

就是截获消息的方式知道插入和拔出