【发布时间】:2017-06-19 20:10:21
【问题描述】:
目前,我正在使用 LibUSBDotNet 来检测何时插入 USB。但是,安装需要几秒钟,所以目前,我正在运行以下代码:
private void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
{
//Log.WriteDiagnostic(e.ToString());
Thread thread = new Thread(USBThreadStart);
thread.Start();
}
private void USBThreadStart()
{
Thread.Sleep(5000);
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
if (drive.Name.Contains("/SomeUsbName"))
Log.WriteDiagnostic("Usb Found.");
}
在此,我启动一个等待 5 秒的线程(足以挂载设备),然后循环遍历所有驱动器以获取具有给定名称的驱动器(我们在 USB 记忆棒上为客户提供专有命名)。我这样做的原因是因为 DriveInfo.GetDrives() 将所有 USB 记忆棒作为固定类型返回,而不是可移动类型。
这行得通,但它肯定不是最好的方法。有没有更好的方法,在 Linux 上使用 Mono C# 来检测 U 盘何时挂载?
【问题讨论】: