UWP usb检测

UWP usb detection

我想在我的应用程序中检测 USB 驱动器(在启动时),但我找不到一个简单的方法来做到这一点!

在 WinForms 中我使用了 DriveInfo 但它在 UWP 中不存在。

我发现了这个:How to get notifications if devices are added, removed, or changed (XAML) 但它一点也不简单!它找到了除我的 USB 驱动器之外的所有内容! (或者它没有显示好名字)。

你能帮帮我吗?谢谢

DeviceEnumerationAndPairing

上查看此样本

如果您希望在您的应用程序打开时执行此操作,请参阅场景 2

如果您希望在您的应用未打开(后台)时执行此操作,请参阅场景 3。

在示例中,当我连接 USB 时,我能够看到设备接口 ID 和名称。这应该可以帮助您入门。

编辑 2:因此根据您的要求,请参阅 KnownFolders.Removable Devices

的文档

这将为您提供新添加的可移动设备中的所有文件和文件夹(在 phone 上使用 OTG 或 SD 卡)

它将为您提供所有便携式设备列表

   var myDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.PortableStorageDevice);

 StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices;
StorageFolder sdCard = (await externalDevices.GetFoldersAsync()).FirstOrDefault();

 // An SD card is present and the sdCard variable now contains a reference to it.
    if (sdCard != null)
      {
        // do whatever you want!
       }