StorageFolder.CreateFolderQuery StorageFolder.CreateFolderQuery StorageFolder.CreateFolderQuery StorageFolder.CreateFolderQuery StorageFolder.CreateFolderQuery Method

Definition

Overloads

CreateFolderQuery() CreateFolderQuery() CreateFolderQuery() CreateFolderQuery() CreateFolderQuery()

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

CreateFolderQuery(CommonFolderQuery) CreateFolderQuery(CommonFolderQuery) CreateFolderQuery(CommonFolderQuery) CreateFolderQuery(CommonFolderQuery) 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() CreateFolderQuery() CreateFolderQuery() CreateFolderQuery() CreateFolderQuery()

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

public : StorageFolderQueryResult CreateFolderQuery()
StorageFolderQueryResult CreateFolderQuery() const;
public StorageFolderQueryResult CreateFolderQuery()
Public Function CreateFolderQuery() As StorageFolderQueryResult
var storageFolderQueryResult = storageFolder.createFolderQuery();

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.

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 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 + "
"; OutputDebugString(output->Begin()); } });
// 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 JPG files in the user's Pictures folder and group them by month.
var CommonFolderQuery = Windows.Storage.Search.CommonFolderQuery;
var groupedItems = picturesFolder.createFolderQuery(CommonFolderQuery.groupByMonth);

// Iterate over the results and print the list of folders
// and files to the Visual Studio Output window.
var subfoldersPromise = groupedItems.getFoldersAsync();
subfoldersPromise.done(function getFoldersSuccess(subfolders) {
    subfolders.forEach(function forEachSubfolder(folder) {
        console.log(folder.name);

        // To iterate over the files in each folder,
        // uncomment the following lines.
        // var filesPromise = folder.getFilesAsync();
        // filesPromise.done(function (files) {
        //     console.log(folder.name);
        //     files.forEach(function forEachFile(file) {
        //         console.log(".", file.name);
        //     });
        // });
    });
});

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

CreateFolderQuery(CommonFolderQuery) CreateFolderQuery(CommonFolderQuery) CreateFolderQuery(CommonFolderQuery) CreateFolderQuery(CommonFolderQuery) 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 : StorageFolderQueryResult CreateFolderQuery(CommonFolderQuery query)
StorageFolderQueryResult CreateFolderQuery(CommonFolderQuery query) const;
public StorageFolderQueryResult CreateFolderQuery(CommonFolderQuery query)
Public Function CreateFolderQuery(query As CommonFolderQuery) As StorageFolderQueryResult
var storageFolderQueryResult = storageFolder.createFolderQuery(query);

Parameters

query
CommonFolderQuery CommonFolderQuery 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&lt;StorageFolder >. Each folder in the list is represented by a StorageFolder object.

Exceptions

UnauthorizedAccessException UnauthorizedAccessException UnauthorizedAccessException

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

ArgumentException ArgumentException ArgumentException

You specified a value other than DefaultQuery from the 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