i working windows phone 8.1(rt) application, wanted know how the number of files inside storagefolder.
i know can use storagefolder.getfilesasync()
, check count of list returned. since method takes long , returns items there more efficient method of getting done?
you can 3 orders of magnitude faster performance if use win32 apis file count, works local storage directory (it won't work brokered locations such pictures or music). example, given following c++/cx component:
header
public ref struct fileperftest sealed { windows::foundation::iasyncoperation<uint32>^ getfilecountwin32async(); uint32 getfilecountwin32(); };
implementation
uint32 fileperftest::getfilecountwin32() { std::wstring localfolderpath(applicationdata::current->localfolder->path->data()); localfolderpath += l"\\test\\*"; uint32 found = 0; win32_find_data finddata{0}; handle hfile = findfirstfileex(localfolderpath.c_str(), findexinfobasic, &finddata, findexsearchnamematch, nullptr, find_first_ex_large_fetch); if (hfile == invalid_handle_value) throw ref new platform::exception(getlasterror(), l"can't findfirstfile"); { if ((finddata.dwfileattributes & file_attribute_directory) == 0) ++found; } while (findnextfile(hfile, &finddata) != 0); auto hr = getlasterror(); findclose(hfile); if (hr != error_no_more_files) throw ref new platform::exception(hr, l"error finding files"); return found; } windows::foundation::iasyncoperation<uint32>^ fileperftest::getfilecountwin32async() { return concurrency::create_async([this] { return getfilecountwin32(); }); }
if test on lumia 920 in release mode 1,000 files, win32 version takes less 5 milliseconds (faster if use non-async version, , @ speed there's no need async) whereas using storagefolder.getfilesasync().count
takes more 6 seconds.
edit 7/1/15
note if targeting windows desktop apps, can use storagefolder.createfilequery
method create bulk query, , should faster. unfortunately isn't supported on phone 8.1