Windows Dev Center

StorageFolder.CreateFileQuery(CommonFileQuery) | createFileQuery(CommonFileQuery) method

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.

Syntax


var storageFileQueryResult = storageFolder.createFileQuery(query);

Parameters

query

Type: CommonFileQuery

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

Return value

Type: StorageFileQueryResult

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<StorageFile>. Each file is represented by an item of type StorageFile.

Exceptions

ExceptionCondition
System.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.

Note  

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.

To get deep query results from a folder that's not a library folder, call the CreateFileQueryWithOptions(QueryOptions) method and specify Deep as the value of the FolderDepth property of the QueryOptions.

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.

To specify additional query options, call the CreateFileQueryWithOptions method.

To get items that are files or folders, call the CreateItemQuery method.

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

Examples

The following example shows how to get the files in the user's Pictures folder and its subfolders, sorted by date, by calling the CreateFileQuery(CommonFileQuery) method.

Before you run the following example, enable the Pictures Library capability in the app manifest file.


// Get the user's Pictures folder.
// Enable the corresponding capability in the app manifest file.
var KnownFolders = Windows.Storage.KnownFolders;
var picturesFolder = KnownFolders.picturesLibrary;

// Get the files in the user's Pictures folder and sort them by date.
var CommonFileQuery = Windows.Storage.Search.CommonFileQuery;
var results = picturesFolder.createFileQuery(CommonFileQuery.orderByDate);

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


Requirements (Windows 10 device family)

Device family

Universal

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0

Namespace

Windows.Storage
Windows::Storage [C++]

Metadata

Windows.Foundation.UniversalApiContract.winmd

Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8

Namespace

Windows.Storage
Windows::Storage [C++]

Metadata

Windows.winmd

See also

StorageFolder
CreateFileQuery
CreateFileQueryWithOptions

 

 

Show:
© 2015 Microsoft