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

折月煮酒 提交于 2019-12-11 05:52:33

问题


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.

Thanks in advance for your help!


回答1:


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

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


来源:https://stackoverflow.com/questions/40112787/using-deviceid-from-windows-devices-enumeration-to-find-the-drive-letter-of-a-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!