简体   繁体   English

获取根存储文件夹中的所有存储文件以及所有子存储文件夹中的所有项目

[英]Get all storage files in root storage folder and all items in all sub storage folders

Surely there is a simple way of getting all files within a folder and all sub-folders in UWP? 当然,有一种简单的方法可以获取UWP中的文件夹中的所有文件以及所有子文件夹吗? I can't seem to find any examples of it anywhere. 我似乎在任何地方都找不到它的任何示例。 Below is my implementation which loops through loops for a finite amount of subfolders but it is a bit ridiculous and prone to error. 下面是我的实现,该实现循环了有限数量的子文件夹,但这有点荒谬并且容易出错。

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...

I figured it out 我想到了

 private async Task GetAllFiles(StorageFolder folder, List<StorageFile> allFiles, IProgress<int> itemsFound)
    {
        foreach (StorageFile file in await folder.GetFilesAsync())
        {
            if (file.FileType == ".ext")
            {
                allFiles.Add(file);

                if (itemsFound != null)
                {
                    itemsFound.Report(itemsFoundCounter);
                }
            }
        }

        foreach (StorageFolder subfolder in await folder.GetFoldersAsync())
        {
            await GetAllFiles(subfolder, allFiles, itemsFound);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM