StorageFolder.GetItemsAsync StorageFolder.GetItemsAsync StorageFolder.GetItemsAsync StorageFolder.GetItemsAsync StorageFolder.GetItemsAsync Method

Definition

Overloads

GetItemsAsync() GetItemsAsync() GetItemsAsync() GetItemsAsync() GetItemsAsync()

Gets the files and subfolders in the current folder.

GetItemsAsync(UInt32, UInt32) GetItemsAsync(UInt32, UInt32) GetItemsAsync(UInt32, UInt32) GetItemsAsync(UInt32, UInt32) GetItemsAsync(UInt32, UInt32)

Gets an index-based range of files and folders from the list of all files and subfolders in the current folder.

GetItemsAsync() GetItemsAsync() GetItemsAsync() GetItemsAsync() GetItemsAsync()

Gets the files and subfolders in the current folder.

public : IAsyncOperation<IVectorView<IStorageItem>> GetItemsAsync()
IAsyncOperation<IVectorView<IStorageItem>> GetItemsAsync() const;
public IAsyncOperation<IReadOnlyList<IStorageItem>> GetItemsAsync()
Public Function GetItemsAsync() As IAsyncOperation<IReadOnlyList<IStorageItem>>(Of IVectorView)
Windows.Storage.StorageFolder.getItemsAsync().done( /* Your success and error handlers */ );

Returns

IAsyncOperation<IReadOnlyList<IStorageItem>> IAsyncOperation<IReadOnlyList<IStorageItem>> IAsyncOperation<IReadOnlyList<IStorageItem>>

When this method completes successfully, it returns a list of the files and folders in the current folder. The list is of type IReadOnlyList&lt;IStorageItem >. Each item in the list is represented by an IStorageItem object.

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

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 files and subfolders in the current folder by calling the GetItemsAsync() method.

using Windows.Storage;
using System.Threading.Tasks;
using System.Diagnostics; // For writing results to Output window.

// Get the app's installation folder.
StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;

// Get the files and folders in the current folder.
IReadOnlyList<IStorageItem> itemsInFolder = await appFolder.GetItemsAsync();

// Iterate over the results and print the list of items
// to the Visual Studio Output window.
foreach (IStorageItem item in itemsInFolder)
{
    if(item.IsOfType(StorageItemTypes.Folder))
        Debug.WriteLine("Folder: " + item.Name);
    else
        Debug.WriteLine("File: " + item.Name + ", " + item.DateCreated);
}
IAsyncAction MainPage::ExampleCoroutineAsync()
{
    // Get the app's installation folder.
    Windows::Storage::StorageFolder appFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };

    // Get the items in the current folder.
    Windows::Foundation::Collections::IVectorView<Windows::Storage::IStorageItem> itemsInFolder{
        co_await appFolder.GetItemsAsync() };

    // Iterate over the results, and print the list of items to the Visual Studio output window.
    for (IStorageItem const& itemInFolder : itemsInFolder)
    {
        std::wstringstream stringstream;

        if (itemInFolder.IsOfType(Windows::Storage::StorageItemTypes::File))
        {
            stringstream << L"File: ";
        }
        else
        {
            stringstream << L"Folder: ";
        }

        stringstream << itemInFolder.Name().c_str() << std::endl;
        ::OutputDebugString(stringstream.str().c_str());
    }
}
// Get the apps installation folder
StorageFolder^ appFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;

// Get the items in the current folder; 
create_task(appFolder->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 + "
"; OutputDebugString(output->Begin()); } else { String^ output = "Folder: " + item->Name + "
"; OutputDebugString(output->Begin()); } } });
// Get the app's installation folder.
var appFolder = Windows.ApplicationModel.Package.current.installedLocation;

// Get the files and folders in the current folder.
var itemsInFolderPromise = appFolder.getItemsAsync();

// Iterate over the results and print the list of items
// to the Visual Studio Output window.
itemsInFolderPromise.done(function getItemsSuccess(itemsInFolder) {
    itemsInFolder.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);
        }
    })
});

Remarks

The following table lists methods of the StorageFolder class that get a list of files and folders. The table identifies shallow queries that only return items from the current folder, and deep queries that return items from the current folder and from its subfolders.

To get deep query results from a folder that's not a library folder, call the CreateItemQueryWithOptions(QueryOptions) method and specify Deep as the value of the FolderDepth property of the QueryOptions object.

MethodCreate a shallow query that only returns items from the current folderCreate a deep query that returns items from the current folder and from its subfolders
GetItemsAsync() Default behavior of this method.N/A
GetItemsAsync(UInt32, UInt32) Default behavior of this method.N/A
CreateItemQuery() Default behavior of this method.N/A
CreateItemQueryWithOptions(QueryOptions)

To get only files, call the GetFilesAsync method. To get only folders, call the GetFoldersAsync method.

See Also

GetItemsAsync(UInt32, UInt32) GetItemsAsync(UInt32, UInt32) GetItemsAsync(UInt32, UInt32) GetItemsAsync(UInt32, UInt32) GetItemsAsync(UInt32, UInt32)

Gets an index-based range of files and folders from the list of all files and subfolders in the current folder.

public : IAsyncOperation<IVectorView<IStorageItem>> GetItemsAsync(unsigned int startIndex, unsigned int maxItemsToRetrieve)
IAsyncOperation<IVectorView<IStorageItem>> GetItemsAsync(uint32_t startIndex, uint32_t maxItemsToRetrieve) const;
public IAsyncOperation<IReadOnlyList<IStorageItem>> GetItemsAsync(UInt32 startIndex, UInt32 maxItemsToRetrieve)
Public Function GetItemsAsync(startIndex As UInt32, maxItemsToRetrieve As UInt32) As IAsyncOperation<IReadOnlyList<IStorageItem>>(Of IVectorView)
Windows.Storage.StorageFolder.getItemsAsync(startIndex, maxItemsToRetrieve).done( /* Your success and error handlers */ );

Parameters

startIndex
UInt32 UInt32 UInt32

The zero-based index of the first item in the range to get.

maxItemsToRetrieve
UInt32 UInt32 UInt32

The maximum number of items to get.

Returns

IAsyncOperation<IReadOnlyList<IStorageItem>> IAsyncOperation<IReadOnlyList<IStorageItem>> IAsyncOperation<IReadOnlyList<IStorageItem>>

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

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

UnauthorizedAccessException UnauthorizedAccessException UnauthorizedAccessException

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

See Also