StorageFolder.GetFilesAsync StorageFolder.GetFilesAsync StorageFolder.GetFilesAsync StorageFolder.GetFilesAsync StorageFolder.GetFilesAsync Method

Definition

Overloads

GetFilesAsync() GetFilesAsync() GetFilesAsync() GetFilesAsync() GetFilesAsync()

Gets the files in the current folder.

GetFilesAsync(CommonFileQuery) GetFilesAsync(CommonFileQuery) GetFilesAsync(CommonFileQuery) GetFilesAsync(CommonFileQuery) 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) GetFilesAsync(CommonFileQuery, UInt32, UInt32) GetFilesAsync(CommonFileQuery, UInt32, UInt32) GetFilesAsync(CommonFileQuery, UInt32, UInt32) 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() GetFilesAsync() GetFilesAsync() GetFilesAsync() GetFilesAsync()

Gets the files in the current folder.

public : IAsyncOperation<IVectorView<StorageFile>> GetFilesAsync()
IAsyncOperation<IVectorView<StorageFile>> GetFilesAsync() const;
public IAsyncOperation<IReadOnlyList<StorageFile>> GetFilesAsync()
Public Function GetFilesAsync() As IAsyncOperation<IReadOnlyList<StorageFile>>(Of IVectorView)
Windows.Storage.StorageFolder.getFilesAsync().done( /* Your success and error handlers */ );
Returns
IAsyncOperation<IReadOnlyList<StorageFile>> IAsyncOperation<IReadOnlyList<StorageFile>> IAsyncOperation<IReadOnlyList<StorageFile>>

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

Exceptions
UnauthorizedAccessException UnauthorizedAccessException UnauthorizedAccessException

You don't have permission to access the contents of the current folder.

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.

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);
// 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 first 20 files in the current folder, sorted by date.
var CommonFileQuery = Windows.Storage.Search.CommonFileQuery;
var sortedItemsPromise = picturesFolder.getFilesAsync(CommonFileQuery.orderByDate, 0, 20);
sortedItemsPromise.done(function getFilesSuccess(sortedItems) {

    // Iterate over the results and print the list of files
    // to the Visual Studio Output window.
    sortedItems.forEach(function forEachFile(file) {
        console.log(file.name, file.dateCreated);
    });
});
// Get user's pictures library
StorageFolder^ picturesLibrary = KnownFolders::PicturesLibrary;

// Get the first 20 sorted images in the library
create_task(picturesLibrary->GetFilesAsync(CommonFileQuery::OrderByDate,0,20)).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()); } });

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.

MethodCreate a shallow query that only returns files from the current folderCreate 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.
See Also

GetFilesAsync(CommonFileQuery) GetFilesAsync(CommonFileQuery) GetFilesAsync(CommonFileQuery) GetFilesAsync(CommonFileQuery) 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.

public : IAsyncOperation<IVectorView<StorageFile>> GetFilesAsync(CommonFileQuery query)
IAsyncOperation<IVectorView<StorageFile>> GetFilesAsync(CommonFileQuery query) const;
public IAsyncOperation<IReadOnlyList<StorageFile>> GetFilesAsync(CommonFileQuery query)
Public Function GetFilesAsync(query As CommonFileQuery) As IAsyncOperation<IReadOnlyList<StorageFile>>(Of IVectorView)
Windows.Storage.StorageFolder.getFilesAsync(query).done( /* Your success and error handlers */ );
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
IAsyncOperation<IReadOnlyList<StorageFile>> IAsyncOperation<IReadOnlyList<StorageFile>> IAsyncOperation<IReadOnlyList<StorageFile>>

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

Exceptions
UnauthorizedAccessException UnauthorizedAccessException UnauthorizedAccessException

You don't have permission to access the contents of the current folder.

See Also

GetFilesAsync(CommonFileQuery, UInt32, UInt32) GetFilesAsync(CommonFileQuery, UInt32, UInt32) GetFilesAsync(CommonFileQuery, UInt32, UInt32) GetFilesAsync(CommonFileQuery, UInt32, UInt32) 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.

public : IAsyncOperation<IVectorView<StorageFile>> GetFilesAsync(CommonFileQuery query, unsigned int startIndex, unsigned int maxItemsToRetrieve)
IAsyncOperation<IVectorView<StorageFile>> GetFilesAsync(CommonFileQuery query, uint32_t startIndex, uint32_t maxItemsToRetrieve) const;
public IAsyncOperation<IReadOnlyList<StorageFile>> GetFilesAsync(CommonFileQuery query, UInt32 startIndex, UInt32 maxItemsToRetrieve)
Public Function GetFilesAsync(query As CommonFileQuery, startIndex As UInt32, maxItemsToRetrieve As UInt32) As IAsyncOperation<IReadOnlyList<StorageFile>>(Of IVectorView)
Windows.Storage.StorageFolder.getFilesAsync(query, startIndex, maxItemsToRetrieve).done( /* Your success and error handlers */ );
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.

startIndex
UInt32 UInt32 UInt32

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

maxItemsToRetrieve
UInt32 UInt32 UInt32

The maximum number of files to retrieve.

Returns
IAsyncOperation<IReadOnlyList<StorageFile>> IAsyncOperation<IReadOnlyList<StorageFile>> IAsyncOperation<IReadOnlyList<StorageFile>>

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

Exceptions
UnauthorizedAccessException UnauthorizedAccessException UnauthorizedAccessException

You don't have permission to access the contents of the current folder.

See Also