【问题标题】:On USB Mount in Mono on LinuxLinux 上 Mono 中的 USB 挂载
【发布时间】: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 盘何时挂载?

【问题讨论】:

    标签: c# linux mono


    【解决方案1】:

    我不知道有哪个图书馆可以做到这一点(我猜你尝试过 google)。我可以提供你实现这样的库。你应该看看here。您可以使用此概念来实现该库。例如,编写一个服务,您可以通过管道接收通知。

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多