当然有一种简单的方法可以获取文件夹中的所有文件以及 UWP 中的所有子文件夹吗?我似乎在任何地方都找不到它的任何例子。下面是我的实现,它循环遍历有限数量的子文件夹,但它有点荒谬并且容易出错。
var libraryFolder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("UserLibrary");
var subFoldersLayer1 = await libraryFolder.GetFoldersAsync();
var rootFiles = await libraryFolder.GetFilesAsync();
List<StorageFile> allFiles = new List<StorageFile>();
IEnumerable<StorageFile> allFilesForScanning = new List<StorageFile>();
foreach (var _file in rootFiles)
{
itemsFoundCounter++;
if (itemsFound != null)
{
itemsFound.Report(itemsFoundCounter);
}
allFiles.Add(_file);
}
foreach (var subFolderLayer1 in subFoldersLayer1)
{
var subFoldersLayer2 = await subFolderLayer1.GetFoldersAsync();
IReadOnlyList<StorageFile> files = await subFolderLayer1.GetFilesAsync();
foreach (var file in files)
{
itemsFoundCounter++;
if (itemsFound != null)
{
itemsFound.Report(itemsFoundCounter);
}
allFiles.Add(file);
}
foreach (var subFolderLayer2 in subFoldersLayer2)
{
var subFoldersLayer3 = await subFolderLayer2.GetFoldersAsync();
files = await subFolderLayer2.GetFilesAsync();
foreach (var file in files)
{
itemsFoundCounter++;
if (itemsFound != null)
{
itemsFound.Report(itemsFoundCounter);
}
allFiles.Add(file);
}
// Goes on like this for a bit...