Reputation: 97
I have a windows 8 program that opens a folder and lists all .txt files in it. The issue I am encountering occurs when a significant number of files are in that folder (thousands). When this happens it causes my program to hang and even crash.
My code looks like this:
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.FileTypeFilter.Add(".txt");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
var folderToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(folder);
var fileList = await folder.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName);
// Do something with the files
Is there something I could do to increase performance (without using the default file picker)? Or should I just put in some form of check to prevent users from opening folders with such a large number of files?
Upvotes: 0
Views: 229
Reputation: 71
If you're having problems with large collections of files, I would recommend requesting the files in batches, rather than all at once.
Use GetItemCountAsync to get the total number of files.
Then call GetFilesAsync multiple times.
GetItemCountAsync();
GetFilesAsync(uint startIndex, uint maxNumberOfItems);
Upvotes: 1