StorageFolder.CreateFileQuery StorageFolder.CreateFileQuery StorageFolder.CreateFileQuery StorageFolder.CreateFileQuery StorageFolder.CreateFileQuery Method

Definition

Overloads

CreateFileQuery() CreateFileQuery() CreateFileQuery() CreateFileQuery() CreateFileQuery()

Gets a query result object that contains the files in the current folder.

CreateFileQuery(CommonFileQuery) CreateFileQuery(CommonFileQuery) CreateFileQuery(CommonFileQuery) CreateFileQuery(CommonFileQuery) CreateFileQuery(CommonFileQuery)

Gets a query result object that contains the files in the current folder. Also gets the files from the subfolders of the current folder when the value of the query argument is something other than CommonFileQuery.DefaultQuery. Files are sorted based on the specified CommonFileQuery.

CreateFileQuery() CreateFileQuery() CreateFileQuery() CreateFileQuery() CreateFileQuery()

Gets a query result object that contains the files in the current folder.

public : StorageFileQueryResult CreateFileQuery()
StorageFileQueryResult CreateFileQuery() const;
public StorageFileQueryResult CreateFileQuery()
Public Function CreateFileQuery() As StorageFileQueryResult
var storageFileQueryResult = storageFolder.createFileQuery();
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.

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 + "
"; OutputDebugString(output->Begin()); } });
// Get the app's installation folder.
var appFolder = Windows.ApplicationModel.Package.current.installedLocation;

// Get the files in the current folder.
var results = appFolder.createFileQuery();

// Iterate over the results and print the list of files
// to the Visual Studio Output window.
var filesInFolderPromise = results.getFilesAsync();
filesInFolderPromise.done(function getFilesSuccess(filesInFolder) {
    filesInFolder.forEach(function forEachFile(item) {
        console.log(item.name);
    });
});

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.

See Also

CreateFileQuery(CommonFileQuery) CreateFileQuery(CommonFileQuery) CreateFileQuery(CommonFileQuery) CreateFileQuery(CommonFileQuery) CreateFileQuery(CommonFileQuery)

Gets a query result object that contains the files in the current folder. Also gets the files from the subfolders of the current folder when the value of the query argument is something other than CommonFileQuery.DefaultQuery. Files are sorted based on the specified CommonFileQuery.

public : StorageFileQueryResult CreateFileQuery(CommonFileQuery query)
StorageFileQueryResult CreateFileQuery(CommonFileQuery query) const;
public StorageFileQueryResult CreateFileQuery(CommonFileQuery query)
Public Function CreateFileQuery(query As CommonFileQuery) As StorageFileQueryResult
var storageFileQueryResult = storageFolder.createFileQuery(query);
Parameters
query
CommonFileQuery CommonFileQuery CommonFileQuery

One of the enumeration values that specifies how to sort the files and determines whether the query is shallow or deep.

Returns

A query result object. Call the GetFilesAsync method of the query result to get the flat list of files, sorted as specified by query. This method returns a list of type IReadOnlyList&lt;StorageFile >. Each file is represented by an item of type StorageFile.

Exceptions
ArgumentException ArgumentException ArgumentException

You specified a value other than DefaultQuery from the CommonFileQuery enumeration for a folder that's not a library folder. Check the value of query.

Remarks

A CommonFileQuery sorts files based on specific file attributes (like title or date) quickly and easily.

When you specify the DefaultQuery option from the CommonFileQuery enumeration, this query is a shallow query that returns only files in the current folder. When you specify another value from the CommonFileQuery enumeration, this query is a deep query that returns a flattened list of files from the current folder and from its subfolders.

Tip

Some of the values from the CommonFileQuery enumeration can only be used with a library folder (such as the Pictures library) or the Homegroup folder. In addition to the DefaultQuery option, you can use only the OrderByName and OrderBySearchRank options with a folder that's not a library folder.

For a list of methods that identifies shallow queries and deep queries, see the Remarks in the topic GetFilesAsync.

To check whether a specific CommonFileQuery is available, call the folder's IsCommonFileQuerySupported method.

You can also get a list of files in the current folder asynchronously by calling one of the GetFilesAsync methods.

For Windows Server 2012 You must install indexer components to use some CommonFileQuery values because indexer components are not installed by default.

See Also