使用C#Windows 10 IoT核心版将文件复制到USB

问题描述

我正在尝试复制在Windows 10 IoT核心版本地文件夹中创建的.csv文件。我尝试了几种方法,但是没有运气。我的最新代码如下:

string aqs = UsbDevice.GetDeviceSelector(0x045E,0x0611);

        var myDevices = await Windows.Devices.Enumeration.Deviceinformation.FindAllAsync(aqs);
        UsbDevice usbDevice;

        try
        {
            if(myDevices == null)
            {
                return;
            }
            usbDevice = await UsbDevice.FromIdAsync(myDevices[0].Id);


            StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFolder sourcef = await localFolder.CreateFolderAsync("My Data",CreationCollisionoption.OpenIfExists);
            IReadOnlyList<StorageFile> filel = await sourcef.GetFilesAsync();

            StorageFolder removableDevices = KNownFolders.RemovableDevices;
            //var externalDrives = await removableDevices.GetFoldersAsync();
            //var drive0 = externalDrives[0];

            //var destFolder = await removableDevices.CreateFolderAsync("My Data",CreationCollisionoption.GenerateUniqueName);

            foreach (StorageFile file in filel)
            {
                await file.copyAsync(removableDevices);
            }
    }

在上面我得到了一个例外:

usbDevice =等待UsbDevice.FromIdAsync(myDevices [0] .Id);

'myDevices [0] .Id'引发了'System.ArgumentOutOfRangeException'类型的异常

我尝试检查它是否为null或不为null。

此操作的目的是基本上将一些文本文件从“本地文件夹”复制到USB驱动器。

解决方法

使用UsbDevice.GetDeviceSelector找不到USB存储,Windows.Devices.Usb命名空间中的方法用于兼容ID为 USB \ MS_COMP_WINUSB 的有效WinUSB设备,但是USB存储设备将不包含兼容的ID。事实上,KnownFolders.RemovableDevices足以访问该设备。