使用DeviceID从Windows. devices . enumeration找到一个可移动驱动器(USB)在c#

本文关键字:可移动 一个 DeviceID 驱动器 USB devices Windows enumeration 使用 | 更新日期: 2023-09-27 18:13:29

我正试图扫描插入运行Windows 10 IoT Core的树莓派的USB驱动器,以查找特定文件夹中的特定文件。我目前能够检测何时使用Windows.Devices.Enumeration.DeviceWatcher从设备中添加或删除USB驱动器,并且也可以使用它找到DeviceId。我使用DeviceEnumerationAndPairing演示项目做到了这一点。我知道StorageDevice.FromId()通常会为此工作,但它似乎不同意Windows 10物联网核心。我也试过使用KnownFolders。RemovableDevices在添加设备时循环遍历所有设备,但这总是立即使应用程序崩溃。我希望能够使用DeviceId来查找USB设备的驱动器号,然后检查驱动器是否为空,如果不是,在该设备中导航到我期望的目录和文件。

提前感谢您的帮助!

使用DeviceID从Windows. devices . enumeration找到一个可移动驱动器(USB)在c#

您可以在DeviceEnumerationAndPairing演示项目的handlerAdded() in scenario2中添加以下代码行。

var removableDevices = KnownFolders.RemovableDevices;
// Get all driver letters
var folders = await removableDevices.GetFoldersAsync();
if (folders.Count > 0)
{
    foreach (StorageFolder folder in folders)
    {
        System.Diagnostics.Debug.WriteLine(folder.Name);
        // Check the driver letter is empty or not.
        var items = folder.GetItemsAsync();
        if (items == null)
        {
            System.Diagnostics.Debug.WriteLine("There are no files and folders.");
        }
    }
}

同时,您需要声明您想要访问的文件类型的Capability,例如图片,您可以在Package.appxmanifest中添加以下行。

这对我有用。希望对你有帮助。

<uap:Capability Name="picturesLibrary" />