Udostępnij za pośrednictwem


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.

public:
 virtual IAsyncOperation<IVectorView<StorageFile ^> ^> ^ GetFilesAsync() = GetFilesAsync;
/// [Windows.Foundation.Metadata.Overload("GetFilesAsyncOverloadDefaultOptionsStartAndCount")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<IVectorView<StorageFile>> GetFilesAsync();
[Windows.Foundation.Metadata.Overload("GetFilesAsyncOverloadDefaultOptionsStartAndCount")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IReadOnlyList<StorageFile>> GetFilesAsync();
function getFilesAsync()
Public Function GetFilesAsync () As IAsyncOperation(Of IReadOnlyList(Of StorageFile))

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.

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);
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Storage.Search.h>
...
IAsyncAction ExampleCoroutineAsync()
{
    // Get the user's Pictures library.
    // Enable the Pictures Library capability in the app manifest file.
    Windows::Storage::StorageFolder picturesLibrary{ Windows::Storage::KnownFolders::PicturesLibrary() };

    // Get the first 20 sorted images in the library, sorted by date.
    Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFile> filesInFolder{
        co_await picturesLibrary.GetFilesAsync(Windows::Storage::Search::CommonFileQuery::OrderByDate, 0, 20) };

    // Iterate over the results, and print the list of files to the Visual Studio output window.
    for (Windows::Storage::StorageFile const& fileInFolder : filesInFolder)
    {
        std::wstring output{ fileInFolder.Name() + L' ' };
        ::OutputDebugString(output.c_str());
    }
    ::OutputDebugString(L"\n");
}
// 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 + "\n";
  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.

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

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:
 virtual IAsyncOperation<IVectorView<StorageFile ^> ^> ^ GetFilesAsync(CommonFileQuery query) = GetFilesAsync;
/// [Windows.Foundation.Metadata.Overload("GetFilesAsyncOverloadDefaultStartAndCount")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<IVectorView<StorageFile>> GetFilesAsync(CommonFileQuery const& query);
[Windows.Foundation.Metadata.Overload("GetFilesAsyncOverloadDefaultStartAndCount")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IReadOnlyList<StorageFile>> GetFilesAsync(CommonFileQuery query);
function getFilesAsync(query)
Public Function GetFilesAsync (query As CommonFileQuery) As IAsyncOperation(Of IReadOnlyList(Of StorageFile))

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

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:
 virtual IAsyncOperation<IVectorView<StorageFile ^> ^> ^ GetFilesAsync(CommonFileQuery query, unsigned int startIndex, unsigned int maxItemsToRetrieve) = GetFilesAsync;
/// [Windows.Foundation.Metadata.Overload("GetFilesAsync")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<IVectorView<StorageFile>> GetFilesAsync(CommonFileQuery const& query, uint32_t const& startIndex, uint32_t const& maxItemsToRetrieve);
[Windows.Foundation.Metadata.Overload("GetFilesAsync")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IReadOnlyList<StorageFile>> GetFilesAsync(CommonFileQuery query, uint startIndex, uint maxItemsToRetrieve);
function getFilesAsync(query, startIndex, maxItemsToRetrieve)
Public Function GetFilesAsync (query As CommonFileQuery, startIndex As UInteger, maxItemsToRetrieve As UInteger) As IAsyncOperation(Of IReadOnlyList(Of StorageFile))

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

unsigned int

uint32_t

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

maxItemsToRetrieve
UInt32

unsigned int

uint32_t

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) M:Windows.Storage.Search.IStorageFolderQueryOperations.GetFilesAsync(Windows.Storage.Search.CommonFileQuery,unsigned int,unsigned int) M:Windows.Storage.Search.IStorageFolderQueryOperations.GetFilesAsync(Windows.Storage.Search.CommonFileQuery,uint32_t,uint32_t)
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