StorageFolder.GetFilesAsync Method

Definition

Overloads

GetFilesAsync()

Gets the files in the current folder.

GetFilesAsync(CommonFileQuery)

Gets 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 value from the CommonFileQuery enumeration.

GetFilesAsync(CommonFileQuery, UInt32, UInt32)

Gets an index-based range of files from the list of all 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 value from the CommonFileQuery enumeration.

GetFilesAsync()

Gets the files in the current folder.

C#
[Windows.Foundation.Metadata.Overload("GetFilesAsyncOverloadDefaultOptionsStartAndCount")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IReadOnlyList<StorageFile>> GetFilesAsync();

Returns

When this method completes successfully, it returns a list of the files in the current folder. The list is of type IReadOnlyList<StorageFile>. Each file in the list is represented by a StorageFile object.

Implements

Attributes

Exceptions

You don't have permission to access the contents of the current folder. For more information, see File access permissions.

Examples

The following example shows how to get the contents of the user's Pictures folder and its subfolders, sorted by date, by calling the GetFilesAsync(CommonFileQuery, UInt32, UInt32) overloaded method. This example returns a maximum of 20 files, starting with the file at index 0. Since the CommonFileQuery.OrderByDate option sorts dates in descending order (that is, from newest to oldest), this example returns the user's 20 most recent photos.

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

C#
using Windows.Storage;
using Windows.Storage.Search;
using System.Threading.Tasks;
using System.Diagnostics; // For writing results to Output window.

// Get the user's Pictures folder.
// Enable the corresponding capability in the app manifest file.
StorageFolder picturesFolder = KnownFolders.PicturesLibrary;

// Get the first 20 files in the current folder, sorted by date.
IReadOnlyList<StorageFile> sortedItems = await picturesFolder.GetFilesAsync(CommonFileQuery.OrderByDate,0,20);

// Iterate over the results and print the list of files
// to the Visual Studio Output window.
foreach (StorageFile file in sortedItems)
    Debug.WriteLine(file.Name + ", " + file.DateCreated);

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.

The following table lists methods of the StorageFolder class that get a list of files. The table identifies shallow queries that only return files from the current folder, and deep queries that return files from the current folder and from its subfolders.

Some methods take a value from the CommonFileQuery enumeration. When you specify the DefaultQuery option from the CommonFileQuery enumeration, the query is a shallow query that returns only files in the current folder. When you specify another value from the CommonFileQuery enumeration, the 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.

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 object.

Method Create a shallow query that only returns files from the current folder Create a deep query that returns files from the current folder and from its subfolders
GetFilesAsync() Default behavior of this method. N/A
GetFilesAsync(CommonFileQuery) Specify the DefaultQuery option. For a library folder, specify an option other than DefaultQuery.
GetFilesAsync(CommonFileQuery, UInt32, UInt32) Specify the DefaultQuery option. For a library folder, specify an option other than DefaultQuery.
CreateFileQuery() Default behavior of this method. N/A
CreateFileQuery(CommonFileQuery) Specify the DefaultQuery option. For a library folder, specify an option other than DefaultQuery.
CreateFileQueryWithOptions(QueryOptions) Default behavior of this method if none of the following options are specified.
- or -
Specify DefaultQuery as the value of CommonFileQuery when you instantiate the QueryOptions object.
- or -
Specify Shallow as the value of the FolderDepth property of the QueryOptions object.
For a library folder, specify a value other than DefaultQuery as the value of CommonFileQuery when you instantiate the QueryOptions object.
- or -
For any folder, specify Deep as the value of the FolderDepth property of the QueryOptions.

See also

Applies to

WinRT Build 26100 and other versions
Product Versions
WinRT Build 10240, Build 10586, Build 14383, Build 15063, Build 16299, Build 17134, Build 17763, Build 18362, Build 19041, Build 20348, Build 22000, Build 22621, Build 26100

GetFilesAsync(CommonFileQuery)

Gets 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 value from the CommonFileQuery enumeration.

C#
[Windows.Foundation.Metadata.Overload("GetFilesAsyncOverloadDefaultStartAndCount")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IReadOnlyList<StorageFile>> GetFilesAsync(CommonFileQuery query);

Parameters

query
CommonFileQuery

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

Returns

When this method completes successfully, it returns a flat list of files, sorted as specified by query. The list is of type IReadOnlyList<StorageFile>. Each file in the list is represented by a StorageFile object.

Implements

Attributes

Exceptions

You don't have permission to access the contents of the current folder. For more information, see File access permissions.

See also

Applies to

WinRT Build 26100 and other versions
Product Versions
WinRT Build 10240, Build 10586, Build 14383, Build 15063, Build 16299, Build 17134, Build 17763, Build 18362, Build 19041, Build 20348, Build 22000, Build 22621, Build 26100

GetFilesAsync(CommonFileQuery, UInt32, UInt32)

Gets an index-based range of files from the list of all 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 value from the CommonFileQuery enumeration.

C#
[Windows.Foundation.Metadata.Overload("GetFilesAsync")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IReadOnlyList<StorageFile>> GetFilesAsync(CommonFileQuery query, uint startIndex, uint maxItemsToRetrieve);

Parameters

query
CommonFileQuery

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

startIndex
UInt32

The zero-based index of the first file in the range to retrieve.

maxItemsToRetrieve
UInt32

The maximum number of files to retrieve.

Returns

When this method completes successfully, it returns a flat list of files sorted as specified by query. The list is of type IReadOnlyList<StorageFile>. Each file in the list is represented by a StorageFile object.

Implements

M:Windows.Storage.Search.IStorageFolderQueryOperations.GetFilesAsync(Windows.Storage.Search.CommonFileQuery,System.UInt32,System.UInt32)
Attributes

Exceptions

You don't have permission to access the contents of the current folder. For more information, see File access permissions.

See also

Applies to

WinRT Build 26100 and other versions
Product Versions
WinRT Build 10240, Build 10586, Build 14383, Build 15063, Build 16299, Build 17134, Build 17763, Build 18362, Build 19041, Build 20348, Build 22000, Build 22621, Build 26100