Skip to main content
StorageFolder.CreateItemQuery | createItemQuery method

Applies to Windows only

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

Syntax


var storageItemQueryResult = storageFolder.createItemQuery();

public StorageItemQueryResult CreateItemQuery()

Public Function CreateItemQuery() As StorageItemQueryResult

public:
StorageItemQueryResult^ CreateItemQuery()

Parameters

This method has no parameters.

Return value

Type: StorageItemQueryResult

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

To work with the returned items, call the IsOfType method of the IStorageItem interface to determine whether each item is a file or a folder. Then cast the item to a StorageFolder or StorageFile.

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 items in the current folder. For a list of methods that identifies shallow queries and deep queries, see the Remarks in the topic GetItemsAsync.

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

To specify additional query options, call the CreateItemQueryWithOptions method.

To get only files, call the CreateFileQuery method. To get only folders, call the CreateFolderQuery method.

Examples

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


using Windows.Storage;
using Windows.Storage.Search;
using System.Threading.Tasks;
using System.Diagnostics; // For writing results to the Output window.
...
            // Get the app's installation folder.
            StorageFolder appFolder =
                Windows.ApplicationModel.Package.Current.InstalledLocation;

            // Get the items in the current folder.
            StorageItemQueryResult itemsInFolder =
                appFolder.CreateItemQuery();

            // Iterate over the results and print the list of items
            // to the Visual Studio Output window.
            foreach (IStorageItem item in await itemsInFolder.GetItemsAsync())
            {
                if(item.IsOfType(StorageItemTypes.Folder))
                    Debug.WriteLine("Folder: " + item.Name);
                else
                    Debug.WriteLine("File: " + item.Name + ", " + item.DateCreated);
            }

// Get the app's installation folder.
var appFolder = Windows.ApplicationModel.Package.current.installedLocation;

// Get the items in the current folder.
var itemsInFolder = appFolder.createItemQuery();

// Iterate over the results and print the list of items
// to the Visual Studio Output window.
var itemsPromise = itemsInFolder.getItemsAsync();
itemsPromise.done(function getItemsSuccess(items) {
    items.forEach(function forEachItem(item) {
        var StorageItemTypes = Windows.Storage.StorageItemTypes;
        if (item.isOfType(StorageItemTypes.folder)) {
            console.log("Folder:", item.name);
        } else {
            console.log("File:", item.name, item.dateCreated);
        }
    });
});

	// Get the apps installation folder
	StorageFolder^ appFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;

	StorageItemQueryResult^ results = appFolder->CreateItemQuery();

	// Get the items in the current folder; 
	create_task(results->GetItemsAsync()).then([=](IVectorView<IStorageItem^>^ itemsInFolder) {
		
		//Iterate over the results and print the list of items
		// to the visual studio output window
		for (auto it = itemsInFolder->First(); it->HasCurrent; it->MoveNext())
		{
			IStorageItem^ item = it->Current;
			if (item->IsOfType(StorageItemTypes::File))
			{
				String^ output = "File: " + item->Name + "\n";
				OutputDebugString(output->Begin());
			}
			else
			{
				String^ output = "Folder: " + item->Name + "\n";
				OutputDebugString(output->Begin());
			}		
		}
	});

Requirements

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