首页 > 解决方案 > 使用 C# Windows 10 iot core 将文件复制到 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”类型的异常

我试过检查这是否为空且不为空。

这样做的目的是基本上将一些文本文件从本地文件夹复制到 USB 驱动器。

标签: c#windows-10-iot-coreusb-drive

解决方案


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


推荐阅读