StorageFolder.CreateFolderQuery Method

Definition

Overloads

CreateFolderQuery()

Gets a query result object that contains the subfolders in the current folder.

CreateFolderQuery(CommonFolderQuery)

Gets a query result object that contains 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.

CreateFolderQuery()

Gets a query result object that contains the subfolders in the current folder.

public:
 virtual StorageFolderQueryResult ^ CreateFolderQuery() = CreateFolderQuery;
/// [Windows.Foundation.Metadata.Overload("CreateFolderQueryOverloadDefault")]
StorageFolderQueryResult CreateFolderQuery();
[Windows.Foundation.Metadata.Overload("CreateFolderQueryOverloadDefault")]
public StorageFolderQueryResult CreateFolderQuery();
function createFolderQuery()
Public Function CreateFolderQuery () As StorageFolderQueryResult

Returns

A query result object. Call the GetFoldersAsync method of the query result to get the subfolders in the current folder. This method returns a list of type IReadOnlyList<StorageFolder>. Each file or folder is represented by an item of type StorageFolder.

Implements

Attributes

Exceptions

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 into folders by month, by calling the GetFoldersAsync(CommonFolderQuery) overloaded method. (Files from the root of the current folder are not included.)

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 the 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.
StorageFolderQueryResult groupedItems =
    picturesFolder.CreateFolderQuery(CommonFolderQuery.GroupByMonth);

// Iterate over the results and print the list of folders
// and files to the Visual Studio Output window.
foreach (StorageFolder folder in await groupedItems.GetFoldersAsync())
{
    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, and group them by month.
    Windows::Storage::Search::StorageFolderQueryResult results{ picturesFolder.CreateFolderQuery(Windows::Storage::Search::CommonFolderQuery::GroupByMonth) };

    Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFolder> itemsInFolder{
        co_await results.GetFoldersAsync() };

    // 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 user's pictures folder
StorageFolder^ picturesFolder = KnownFolders::PicturesLibrary;

// Get the files in the user's Pictures folder and group them by month
StorageFolderQueryResult^ itemsInFolder = picturesFolder->CreateFolderQuery(CommonFolderQuery::GroupByMonth);

create_task(itemsInFolder->GetFoldersAsync()).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());
 }
});

Remarks

This query is a shallow query that returns only subfolders in the current folder. For a list of methods that identifies shallow queries and deep queries, see the Remarks in the GetFoldersAsync method.

To specify additional query options, call the CreateFolderQueryWithOptions method.

To get items that are files or folders, call the CreateItemQuery method.

See also

Applies to

CreateFolderQuery(CommonFolderQuery)

Gets a query result object that contains 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.

public:
 virtual StorageFolderQueryResult ^ CreateFolderQuery(CommonFolderQuery query) = CreateFolderQuery;
/// [Windows.Foundation.Metadata.Overload("CreateFolderQuery")]
StorageFolderQueryResult CreateFolderQuery(CommonFolderQuery const& query);
[Windows.Foundation.Metadata.Overload("CreateFolderQuery")]
public StorageFolderQueryResult CreateFolderQuery(CommonFolderQuery query);
function createFolderQuery(query)
Public Function CreateFolderQuery (query As CommonFolderQuery) As StorageFolderQueryResult

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

A query result object. Call the GetFoldersAsync method of the query result to get the subfolders in the current folder. When the value of the query argument is something other than CommonFolderQuery.DefaultQuery, the query result object contains 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<StorageFolder>. Each folder in the list is represented by a StorageFolder object.

Implements

Attributes

Exceptions

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

You specified a value other than DefaultQuery from the <xref:Windows.Storage.Search.CommonFolderQuery?text=CommonFolderQuery> enumeration for a folder that's not a library folder. Check the value of query.

Remarks

A CommonFolderQuery groups the contents of subfolders into folders based on specific file attributes (like artist or album) quickly and easily. See the Remarks on the CreateFileQuery method page for more information about specifying DefaultQuery options.

See also

Applies to