I am letting a user chose a folder on the device through topLevel.StorageProvider.OpenFolderPickerAsync
. When the folder gets returned succesfully, i use the following code to try to read all subfolders in that directory.
The first time i call loadFilesRecursively
and GetItemsAsync
, it gives the correct list of folders, but when i call loadFilesRecursively
on a subfolder, GetItemsAsync
returns the previous result, so it keeps looping on the first folder it finds. (I have read permissions for external storage)
private async void loadFilesRecursively(string parentDirectory)
{
Debug.WriteLine("Loading path: " + parentDirectory);
// Get all directories in current dir
// Start async operation to open the dialog.
// Get top level from the current control. Alternatively, you can use Window reference instead.
TopLevel topLevel = TopLevel.GetTopLevel(this);
IStorageFolder iFolder = await topLevel.StorageProvider.TryGetFolderFromPathAsync(new Uri(parentDirectory));
List<IStorageItem> filesAndFolders = iFolder.GetItemsAsync().ToBlockingEnumerable().ToList();
foreach (IStorageItem file in filesAndFolders)
{
if (file.Path.ToString() == parentDirectory)
{
Debug.WriteLine("Read sub folder is same as parent...skipping");
continue;
}
Debug.WriteLine("Read sub folder " + file.Name);
loadFilesRecursively(file.Path.ToString());
}
}
Edit: even just manually trying to add a second part to get second level folders with a second “GetItemsAsync”, it just doesnt work:
private async void loadFilesRecursively(string parentDirectory)
{
Debug.WriteLine("Loading path: " + parentDirectory);
// Get all directories in current dir
// Start async operation to open the dialog.
// Get top level from the current control. Alternatively, you can use Window reference instead.
TopLevel topLevel = TopLevel.GetTopLevel(this);
IStorageFolder iFolder = await topLevel.StorageProvider.TryGetFolderFromPathAsync(new Uri(parentDirectory));
List<IStorageItem> filesAndFolders = iFolder.GetItemsAsync().ToBlockingEnumerable().ToList();
foreach (IStorageItem file in filesAndFolders)
{
if (file.Path.ToString() == parentDirectory)
{
Debug.WriteLine("Read sub folder is same as parent...skipping");
continue;
}
Debug.WriteLine("Read sub folder " + file.Name);
// loadFilesRecursively(file.Path.ToString());
TopLevel topLevel2 = TopLevel.GetTopLevel(this);
string parentDirectory2 = file.Path.ToString();
IStorageFolder iFolder2 = await topLevel2.StorageProvider.TryGetFolderFromPathAsync(new Uri(parentDirectory2));
List<IStorageItem> filesAndFolders2 = iFolder2.GetItemsAsync().ToBlockingEnumerable().ToList();
foreach (IStorageItem file2 in filesAndFolders2)
{
if (file2.Path.ToString() == parentDirectory2)
{
Debug.WriteLine("Read sub folder is same as parent...skipping");
continue;
}
Debug.WriteLine("Read sub folder " + file2.Name);
// loadFilesRecursively(file.Path.ToString());
}
}
}
EDIT 2: It only happens on android, on Desktop it works fine