Retrieve files and folders recursively in Windows Store apps

Earlier in Windows Forms applications, this was what I was looking for a folder recursively for all files. I know that Windows Store apps are pretty much isolated, but there should be a way to get all the files in the KnownFolder directory. I tried to do this using the music catalog. However, this does not work for me. I made my Googling, and I cannot find a thread that says how to achieve this. I tried the following code:

private async void dirScan(string dir)
    {
        var folDir = await StorageFolder.GetFolderFromPathAsync(dir);
        foreach (var d in await folDir.GetFoldersAsync())
        {
            foreach(var f in await d.GetFilesAsync())
            {
                knownMusicDir.Add(f.Path.ToString());
            }
            dirScan(d.ToString());
        }
    }

I hope someone can take a look at my code and hopefully fix it. Thanks in advance!

+4
source share
2

KnownFolders:

ObservableCollection<string> files; 

public MainPage()
{
    this.InitializeComponent();
    files = new ObservableCollection<string>();
}

private async void GetFiles(StorageFolder folder)
{
    StorageFolder fold = folder;

    var items = await fold.GetItemsAsync();

    foreach (var item in items)
    {
        if (item.GetType() == typeof(StorageFile))
            files.Add(item.Path.ToString());
        else
            GetFiles(item as StorageFolder);
    }

    listView.ItemsSource = files;      
}
+3

:

public static async Task<IEnumerable<StorageFile>> GetAllFilesAsync(this StorageFolder folder)
{
        IEnumerable<StorageFile> files = await folder.GetFilesAsync();
        IEnumerable<StorageFolder> folders = await folder.GetFoldersAsync();
        foreach (StorageFolder subfolder in folders)
            files = files.Concat(await subfolder.GetAllFilesAsync());
        return files;
}
+4

Source: https://habr.com/ru/post/1530607/


All Articles