使用Windows.Devices.Enumeration中的DeviceID在C#Windows 10 IoT核心版中查找可移动驱动器(USB)的驱动器号? [英] Using DeviceID from Windows.Devices.Enumeration to find the Drive Letter of a Removable Drive (USB) in C# Windows 10 IoT Core?

查看:454
本文介绍了使用Windows.Devices.Enumeration中的DeviceID在C#Windows 10 IoT核心版中查找可移动驱动器(USB)的驱动器号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扫描插入Windows 10 IoT核心版的Raspberry Pi的USB驱动器,以查找特定文件夹中的特定文件。我目前能够使用Windows.Devices.Enumeration.DeviceWatcher检测何时将USB驱动器添加到设备或从设备中移除,也可以使用该设备找到DeviceId。我使用 DeviceEnumerationAndPairing 演示项目进行了此操作。我知道StorageDevice.FromId()通常可以正常工作,但似乎与Windows 10 IoT核心版本不同。我还尝试过使用KnownFolders.RemovableDevices在添加设备时遍历所有设备,但总是立即使应用程序崩溃。我希望能够使用DeviceId查找USB设备的驱动器号,然后检查驱动器是否为空,如果不是,请在该设备中导航到我希望存在的目录和文件。

I am trying to scan a USB drive that is inserted into a Raspberry Pi running Windows 10 IoT Core for a specific file in a specific folder. I am currently able to detect when a USB drive is added or removed from the device using Windows.Devices.Enumeration.DeviceWatcher and can find the DeviceId using that as well. I did this using the DeviceEnumerationAndPairing demo project. I am aware that StorageDevice.FromId() would work for this usually but it seems to not agree with Windows 10 IoT Core. I have also tried using KnownFolders.RemovableDevices to loop through all the devices whenever a device is added but that always immediately crashes the application. I want to be able to use the DeviceId to find the drive letter of the USB device and then check if the drive is empty or not and if not, navigate in that device to the directory and file I expect to be there.

预先感谢您的帮助!

推荐答案

您可以添加以下代码handlerAdded()中的行

You can add the following code lines in the handlerAdded() in scenario2 of the DeviceEnumerationAndPairing demo project.

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.");
        }
    }
}

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

Meanwhile, you need declare the Capability of file type you want to access, pictures, for example, you can add the following line in the Package.appxmanifest.

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

This works for me. Hope it is helpful for you.

<uap:Capability Name="picturesLibrary" />

这篇关于使用Windows.Devices.Enumeration中的DeviceID在C#Windows 10 IoT核心版中查找可移动驱动器(USB)的驱动器号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆