StorageFolder.GetFoldersAsync Method

Definition

Overloads

GetFoldersAsync()

Gets the subfolders in the current folder.

GetFoldersAsync(CommonFolderQuery)

Gets the subfolders in the current folder. When the value of the query argument is something other than CommonFolderQuery.DefaultQuery, gets a list of virtual folders that represent containers for groups of files in the subfolders of the current folder. Files are grouped into folders based on the specified value from the CommonFolderQuery enumeration.

GetFoldersAsync(CommonFolderQuery, UInt32, UInt32)

Gets an index-based range of folders from the list of all subfolders in the current folder. When the value of the query argument is something other than CommonFolderQuery.DefaultQuery, gets a list of virtual folders that represent containers for groups of files in the subfolders of the current folder. Files are grouped into folders based on the specified value from the CommonFolderQuery enumeration.

GetFoldersAsync()

Gets the subfolders in the current folder.

C#
public IAsyncOperation<IReadOnlyList<StorageFolder>> GetFoldersAsync()
Returns
IAsyncOperation<IReadOnlyList<StorageFolder>>

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

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

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 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);
}
C++
// 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)).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 + "
";
  OutputDebugString(output->Begin());
 }
});

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.

MethodCreate a shallow query that only returns subfolders from the current folderCreate 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 QueryOptionsobject. - or -
  • Specify Shallow as the value of the FolderDepth property of the QueryOptions object.
See Also

GetFoldersAsync(CommonFolderQuery)

Gets the subfolders in the current folder. When the value of the query argument is something other than CommonFolderQuery.DefaultQuery, gets a list of virtual folders that represent containers for groups of files in the subfolders of the current folder. Files are grouped into folders based on the specified value from the CommonFolderQuery enumeration.

C#
public IAsyncOperation<IReadOnlyList<StorageFolder>> GetFoldersAsync(CommonFolderQuery query)
Parameters
query
CommonFolderQuery

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

Returns
IAsyncOperation<IReadOnlyList<StorageFolder>>

When this method completes successfully, it returns a list of subfolders. When the value of the query argument is something other than CommonFolderQuery.DefaultQuery, this method returns a list of virtual folders that represent containers for groups of files in the subfolders of the current folder. (Files from the current folder are not included.) The files are grouped as specified by query. The list is of type IReadOnlyList&lt;StorageFolder >. Each folder in the list is represented by a StorageFolder object.

Exceptions
UnauthorizedAccessException

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

See Also

GetFoldersAsync(CommonFolderQuery, UInt32, UInt32)

Gets an index-based range of folders from the list of all subfolders in the current folder. When the value of the query argument is something other than CommonFolderQuery.DefaultQuery, gets a list of virtual folders that represent containers for groups of files in the subfolders of the current folder. Files are grouped into folders based on the specified value from the CommonFolderQuery enumeration.

C#
public IAsyncOperation<IReadOnlyList<StorageFolder>> GetFoldersAsync(CommonFolderQuery query, UInt32 startIndex, UInt32 maxItemsToRetrieve)
Parameters
query
CommonFolderQuery

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

startIndex
UInt32

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

maxItemsToRetrieve
UInt32

The maximum number of folders to retrieve.

Returns
IAsyncOperation<IReadOnlyList<StorageFolder>>

When this method completes successfully, it returns a list of subfolders. When the value of the query argument is something other than CommonFolderQuery.DefaultQuery, this method returns a list of virtual folders that represent containers for groups of files in the subfolders of the current folder. (Files from the current folder are not included.) The files are grouped as specified by query. The list is of type IReadOnlyList&lt;StorageFolder >. Each folder in the list is represented by a StorageFolder object.

Exceptions
UnauthorizedAccessException

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

See Also


+ Recent posts