I was working with a Windows Phone 8.1(RT) application, I wanted to know how to get the number of files inside a StorageFolder.
I know we can use StorageFolder.GetFilesAsync()
and then check the count of this list returned.
But since this method takes too long and returns all items is there more efficient method of getting this done?
You can get 3 orders of magnitude faster performance if you use Win32 APIs to get the file count, but it only works for your local storage directory (it won't work for brokered locations such as Pictures or Music). For example, given the following C++/CX component:
Header
Implementation
If I test this on my Lumia 920 in Release mode to get 1,000 files, the Win32 version takes less than 5 milliseconds (faster if you use the non-async version, and at that speed there's really no need to be async) whereas using
StorageFolder.GetFilesAsync().Count
takes more than 6 seconds.Edit 7/1/15
Note that if you are targeting Windows Desktop apps, you can use the
StorageFolder.CreateFileQuery
method to create a bulk query, and that should be faster. But unfortunately it isn't supported on Phone 8.1