繁体   English   中英

使用 Windows.Devices.Enumeration 中的 DeviceID 在 C# Windows 10 IoT Core 中查找可移动驱动器 (USB) 的驱动器号?

[英]Using DeviceID from Windows.Devices.Enumeration to find the Drive Letter of a Removable Drive (USB) in C# Windows 10 IoT Core?

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

在此先感谢您的帮助!

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

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" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM