UWP - Windows IOT - 从 USB 驱动器中名为 "ad_runner" 的特定文件夹获取文件
UWP - Windows IOT - Getting files from a specific folder named "ad_runner" located in a USB drive
当我尝试访问位于 USB 驱动器的名为 "ad_runner" 的文件夹中的文件时,出现未处理的错误。我做错了什么?
async void AccessTheWebAsync()
{
StorageFolder remd = await KnownFolders.RemovableDevices.GetFolderAsync("ad_runner");
IReadOnlyList<StorageFile> fileList = await remd.GetFilesAsync();
foreach (StorageFile file in fileList)
{
dir_lbl.Text = dir_lbl.Text + "\r\n" + file.Name;
}
}
如果您尝试调用 await KnownFolders.RemovableDevices.GetFoldersAsync();
,您会发现第一次调用不是 return 可移动设备中的文件夹,而是可移动设备本身。所以,我稍微修改了你的代码,首先检索所有连接的可移动设备,然后检查名为 "ad_runner" 的内部文件夹:
async void AccessTheWebAsync()
{
//Maybe you want to do this inside a try catch and verify that at least 1 device is connected before you go on....
var folderList = await KnownFolders.RemovableDevices.GetFoldersAsync();
foreach (var device in await KnownFolders.RemovableDevices.GetFoldersAsync())
{
try
{
StorageFolder remd = await device.GetFolderAsync("ad_runner");
IReadOnlyList<StorageFile> fileList = await remd.GetFilesAsync();
foreach (StorageFile file in fileList)
{
//Your logic here....
dir_lbl.Text = dir_lbl.Text + "\r\n" + file.Name;
}
}
catch (Exception)
{
}
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
rightGridWrapper.Children.Add(new TextBlock { Text = $"DeviceName: {device.Name}" });
});
}
}
当我尝试访问位于 USB 驱动器的名为 "ad_runner" 的文件夹中的文件时,出现未处理的错误。我做错了什么?
async void AccessTheWebAsync()
{
StorageFolder remd = await KnownFolders.RemovableDevices.GetFolderAsync("ad_runner");
IReadOnlyList<StorageFile> fileList = await remd.GetFilesAsync();
foreach (StorageFile file in fileList)
{
dir_lbl.Text = dir_lbl.Text + "\r\n" + file.Name;
}
}
如果您尝试调用 await KnownFolders.RemovableDevices.GetFoldersAsync();
,您会发现第一次调用不是 return 可移动设备中的文件夹,而是可移动设备本身。所以,我稍微修改了你的代码,首先检索所有连接的可移动设备,然后检查名为 "ad_runner" 的内部文件夹:
async void AccessTheWebAsync()
{
//Maybe you want to do this inside a try catch and verify that at least 1 device is connected before you go on....
var folderList = await KnownFolders.RemovableDevices.GetFoldersAsync();
foreach (var device in await KnownFolders.RemovableDevices.GetFoldersAsync())
{
try
{
StorageFolder remd = await device.GetFolderAsync("ad_runner");
IReadOnlyList<StorageFile> fileList = await remd.GetFilesAsync();
foreach (StorageFile file in fileList)
{
//Your logic here....
dir_lbl.Text = dir_lbl.Text + "\r\n" + file.Name;
}
}
catch (Exception)
{
}
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
rightGridWrapper.Children.Add(new TextBlock { Text = $"DeviceName: {device.Name}" });
});
}
}