gpt4 book ai didi

c# - 在 Linux 上以 Mono 方式挂载 USB

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:23:37 26 4
gpt4 key购买 nike

目前,我正在使用 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() 将所有 U 盘返回为固定类型,而不是可移动类型。

这行得通,但感觉肯定不是最好的方法。有没有更好的方法,在 Linux 上使用 Mono C# 来检测何时安装了 USB 内存棒?

最佳答案

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

关于c# - 在 Linux 上以 Mono 方式挂载 USB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39020337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com