Windows.Storage.StorageFolder.CreateFileQuery
December 9, 2020 ยท View on GitHub
-description
Gets a query result object that contains the files in the current folder.
-returns
A query result object. Call the GetFilesAsync method of the query result to get the flat list of files. This method returns a list of type IReadOnlyList<StorageFile>. Each file is represented by an item of type StorageFile.
-remarks
This query is a shallow query that returns only files in the current folder. For a list of methods that identifies shallow queries and deep queries, see the Remarks in the topic GetFilesAsync.
You can also get a list of files in the current folder asynchronously by calling one of the GetFilesAsync methods.
To specify additional query options, call the CreateFileQueryWithOptions method.
To get items that are files or folders, call the CreateItemQuery method.
-examples
The following example gets a query result object that contains the files in the current folder by calling the CreateFileQuery() method.
using Windows.Storage;
using Windows.Storage.Search;
using System.Threading.Tasks;
using System.Diagnostics; // For writing results to Output window.
// Get the app's installation folder.
StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// Get the files in the current folder.
StorageFileQueryResult results = appFolder.CreateFileQuery();
// Iterate over the results and print the list of files
// to the Visual Studio Output window.
IReadOnlyList<StorageFile> filesInFolder = await results.GetFilesAsync();
foreach (StorageFile item in filesInFolder)
{
Debug.WriteLine(item.Name);
}
IAsyncAction MainPage::ExampleCoroutineAsync()
{
// Get the app's installation folder.
Windows::Storage::StorageFolder appFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };
// Get the files in the current folder.
Windows::Storage::Search::StorageFileQueryResult results{ appFolder.CreateFileQuery() };
Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFile> filesInFolder{
co_await results.GetFilesAsync() };
// Iterate over the results, and print the list of files to the Visual Studio output window.
for (StorageFile const& fileInFolder : filesInFolder)
{
std::wstring output{ fileInFolder.Name() };
::OutputDebugString(output.c_str());
}
}
//Get the app's installation folder
StorageFolder^ appFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;
//Get the files in the current folder
StorageFileQueryResult^ results = appFolder->CreateFileQuery();
create_task(results->GetFilesAsync()).then([=](IVectorView<StorageFile^>^ filesInFolder) {
//Iterate over the results and print the list of files
// to the visual studio output window
for (auto it = filesInFolder->First(); it->HasCurrent; it->MoveNext())
{
StorageFile^ file = it->Current;
String^ output = file->Name + "\n";
OutputDebugString(output->Begin());
}
});
-see-also
CreateFileQuery(CommonFileQuery), CreateFileQueryWithOptions