Skip to main content
StorageFolder.CreateFolderQuery() | createFolderQuery() method

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

Syntax


var storageFolderQueryResult = storageFolder.createFolderQuery();

public StorageFolderQueryResult CreateFolderQuery()

Public Function CreateFolderQuery() As StorageFolderQueryResult

public:
StorageFolderQueryResult^ CreateFolderQuery()

Parameters

This method has no parameters.

Return value

Type: StorageFolderQueryResult

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

ExceptionCondition
System.UnauthorizedAccessException

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

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 topic GetFoldersAsync.

You can also get a list of subfolders in the current folder asynchronously by calling one of the GetFoldersAsync methods.

To specify additional query options, call the CreateFolderQueryWithOptions method.

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

Examples

The following example shows how to get the subfolders in the current folder by calling the CreateFolderQuery() method.

Before you run the following example, enable the Pictures Library capability in the app manifest file.


using Windows.Storage;
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 subfolders in the user's Pictures folder.
            StorageFolderQueryResult itemsInFolder =
                picturesFolder.CreateFolderQuery();

            // Iterate over the results and print the list of folders
            // and files to the Visual Studio Output window.
            foreach (StorageFolder folder in await itemsInFolder.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);
            }

// 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 subfolders in the user's Pictures folder.
var itemsInFolder = picturesFolder.createFolderQuery();

// Iterate over the results and print the list of folders
// and files to the Visual Studio Output window.
var subfoldersPromise = itemsInFolder.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);
        //     });
        // });
    });
});

 // Get user's pictures folder
 StorageFolder^ picturesFolder = KnownFolders::PicturesLibrary;

 StorageFolderQueryResult^ itemsInFolder = picturesFolder->CreateFolderQuery();
 create_task(itemsInFolder->GetFoldersAsync()).then([=](IVectorView<StorageFolder^>^ itemsInFolder) {
  //Iterate over the results and print the list of files
  // 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());
  }
 });

Requirements (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0

Namespace

Windows.Storage
Windows::Storage [C++]

Metadata

Windows.Foundation.UniversalApiContract.winmd

Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

None supported

Namespace

Windows.Storage
Windows::Storage [C++]

Metadata

Windows.winmd

See also

StorageFolder
CreateFolderQuery(CommonFolderQuery)