【发布时间】:2020-09-11 06:20:39
【问题描述】:
我正在尝试复制在 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”的异常
我已尝试检查 this 是否为 null 且不为 null。
这样做的目的是基本上将一些文本文件从本地文件夹复制到 USB 驱动器。
【问题讨论】:
-
错误并没有说它是空的。它说它是空的。所以,我猜你的设备还没有找到。也许尝试将
if(myDevices == null)更改为if(myDevices is null || !myDevices.Any()) -
如果我在开始调试之前连接设备,windows 10 iot core default app会显示它。所以设备被windows识别。但是,按上述方式更改代码可能仍无法解决问题。还是谢谢!
-
至少上面的异常不会是原因。所以你会更进一步:)
-
顺便说一句:“您的设备尚未找到”我的意思是:从字面上看,您的设备尚未找到或可能与您预期的选择器不同?
标签: c# windows-10-iot-core usb-drive