Skip to content

Latest commit

 

History

History
119 lines (93 loc) · 8.23 KB

storagefolder_getfoldersasync_592765033.md

File metadata and controls

119 lines (93 loc) · 8.23 KB
-api-id -api-type
M:Windows.Storage.StorageFolder.GetFoldersAsync
winrt method

Windows.Storage.StorageFolder.GetFoldersAsync

-description

Gets the subfolders in the current folder.

-returns

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

-exceptions

T:System.UnauthorizedAccessException

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

-remarks

This query is a shallow query that returns only subfolders in the current folder.

The following table lists methods of the StorageFolder class that get a list of subfolders. The table identifies shallow queries that only return subfolders from the current folder, and deep queries that return the contents of nested subfolders, grouped into virtual folders.

Some methods take a value from the CommonFolderQuery enumeration.

  • When you use the DefaultQuery option with any folder, the query returns a list of subfolders in the file system.
  • When you use an option other than DefaultQuery with a library folder, the query returns a list of virtual folders that represent containers for files from the subfolders of the current folder. (Files from the current folder are not included.) The files are grouped into virtual folders based on the specified value from the CommonFolderQuery enumeration. For example, if you specify GroupByMonth, the query returns a list of virtual folders such as July 2014, August 2014, and September 2014.

Tip

You can use the DefaultQuery option with any folder; you can use the other options from the CommonFolderQuery enumeration only with library folders, such as the Pictures library, or the Homegroup folder.

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

Method Create a shallow query that only returns subfolders from the current folder Create a deep query that returns all nested subfolders
GetFoldersAsync() Default behavior of this method. N/A
GetFoldersAsync(CommonFileQuery) Specify the DefaultQuery option. For a library folder, specify an option other than DefaultQuery.
GetFoldersAsync(CommonFileQuery, UInt32, UInt32) Specify the DefaultQuery option. For a library folder, specify an option other than DefaultQuery.
CreateFolderQuery() Default behavior of this method. N/A
CreateFolderQuery(CommonFileQuery) Specify the DefaultQuery option. For a library folder, specify an option other than DefaultQuery.
CreateFolderQueryWithOptions(QueryOptions) Default behavior of this method if none of the following options are specified.
- or -
Specify DefaultQuery as the value of CommonFolderQuery 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 CommonFolderQuery when you instantiate the QueryOptions object.
- or -
For any folder, specify Deep as the value of the FolderDepth property of the QueryOptions.

-examples

The following example shows how to get the contents of the subfolders in the user's Pictures folder, grouped by month, by calling the GetFoldersAsync(CommonFolderQuery, UInt32, UInt32) method. (Files from the root of the current folder are not included.) This example returns a maximum of 4 folders, starting with the folder at index 0. Since the CommonFolderQuery.GroupByMonth option sorts dates in descending order (that is, from newest to oldest), this example returns folders for the 4 most recent months for which the user has photos. Each folder contains all the user's photos from that month.

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 files in the subfolders of the user's Pictures folder,
// grouped by month. Get only the first 4 folders (months).
IReadOnlyList <StorageFolder> groupedItems = await picturesFolder.GetFoldersAsync(CommonFolderQuery.GroupByMonth, 0, 4);

// Iterate over the results and print the list of folders
// and files to the Visual Studio Output window.
foreach (StorageFolder folder in groupedItems)
{
    Debug.WriteLine(folder.Name);

    // To iterate over the files in each folder, uncomment the following lines. 
    // foreach(StorageFile file in await folder.GetFilesAsync())
    //    Debug.WriteLine(" " + file.Name);
}
IAsyncAction MainPage::ExampleCoroutineAsync()
{
    // Get the users's Pictures folder.
    // Enable the Pictures Library capability in the app manifest file.
    Windows::Storage::StorageFolder picturesFolder{ Windows::Storage::KnownFolders::PicturesLibrary() };

    // Get the files in the user's Pictures folder, grouped by month.
    // Get only the first 4 folders (months).
    Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFolder> itemsInFolder{
        co_await picturesFolder.GetFoldersAsync(Windows::Storage::Search::CommonFolderQuery::GroupByMonth, 0, 4) };

    // Iterate over the results, and print the list of file groups to the Visual Studio output window.
    for (StorageFolder const& itemInFolder : itemsInFolder)
    {
        std::wstring output{ itemInFolder.Name() };
        ::OutputDebugString(output.c_str());
    }
}
// Get the user's Pictures folder.
// Enable the corresponding capability in the app manifest file.
StorageFolder^ picturesFolder = KnownFolders::PicturesLibrary;

// Get the files in the user's Pictures folder, grouped by month.
// Get only the first 4 folders (months).
create_task(picturesFolder->GetFoldersAsync(CommonFolderQuery::GroupByMonth, 0, 4)).then([=](IVectorView<StorageFolder^>^ itemsInFolder) {
 //Iterate over the results and print the list of file groups
 // to the visual studio output window
 for (auto it = itemsInFolder->First(); it->HasCurrent; it->MoveNext())
 {
  StorageFolder^ file = it->Current;
  String^ output = file->Name + "\n";
  OutputDebugString(output->Begin());
 }
});

-see-also

File access permissions, GetFoldersAsync(CommonFolderQuery, UInt32, UInt32), GetFoldersAsync(CommonFolderQuery), GetItemsAsync