Bailey Miller Bailey Miller - 14 days ago 12
C# Question

C# UWP StorageFolder not getting any data back with get folders async

I followed a Microsoft example on how to access folders that were selected with a folder picker. After doing this I wanted to get all the paths of the sub folders within the selected top folder. I don't get any results however.

Code:

var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
folderPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();

if (folder != null)
{
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("GameFilesToken", folder);

System.Diagnostics.Debug.WriteLine(folder.Path);


await FileManagementHelper.getFolders(folder.Path);
}

public static async Task<Queue<string>> getFolders(String TopLevel)
{

var Folder = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFolderAsync("GameFilesToken");

var queryResults = Folder.CreateFolderQuery();

var folderList = await queryResults.GetFoldersAsync();


return new Queue<string>();
}


QueryResults gives me access to a folder, the same folder that I use originally.
FolderList gives me nothing.

Answer

FolderList gives me nothing.

Your getting folderList code snippet can work well, folderList object do has values if the selected folder have sub folders. Please update your getfolders method to check the folderList by code as follows:

   var Folder = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFolderAsync("GameFilesToken");
   var queryResults = Folder.CreateFolderQuery();
   var folderList = await queryResults.GetFoldersAsync();
   foreach (StorageFolder folder in  folderList )
   {
       Debug.WriteLine(folder.Path); 
   }

I wanted to get all the paths of the sub folders within the selected top folder. I don't get any results however.

Actually, you can just use StorageFolder.GetFoldersAsync() method to get all sub folders for the selected top folder object, there is no need to use CreateFolderQuery() method if you want to list all. Code as follows:

private async void btngetfolder_Click(object sender, RoutedEventArgs e)
{
    var folderPicker = new Windows.Storage.Pickers.FolderPicker();
    folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
    folderPicker.FileTypeFilter.Add("*");
    folderPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
    Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();

    if (folder != null)
    {
        IReadOnlyList<StorageFolder> folderList = await folder.GetFoldersAsync();
        foreach (StorageFolder subfolder in folderList)
        {
            Debug.WriteLine("subfolder path:" + subfolder.Path);
        } 
    }
}

More details please reference the FolderEnumeration official sample.