Udostępnij za pośrednictwem


StorageFolder.GetItemsAsync Method

Definition

Overloads

GetItemsAsync()

Gets the files and subfolders in the current folder.

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

Gets the files and subfolders in the current folder.

public:
 virtual IAsyncOperation<IVectorView<IStorageItem ^> ^> ^ GetItemsAsync() = GetItemsAsync;
/// [Windows.Foundation.Metadata.Overload("GetItemsAsyncOverloadDefaultStartAndCount")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<IVectorView<IStorageItem>> GetItemsAsync();
[Windows.Foundation.Metadata.Overload("GetItemsAsyncOverloadDefaultStartAndCount")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IReadOnlyList<IStorageItem>> GetItemsAsync();
function getItemsAsync()
Public Function GetItemsAsync () As IAsyncOperation(Of IReadOnlyList(Of IStorageItem))

Returns

When this method completes successfully, it returns a list of the files and folders in the current folder. The list is of type IReadOnlyList<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.

Implements

Attributes

Exceptions

You don't have permission to access the contents of the current folder. For more information, see File access permissions.

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 + "\n";
   OutputDebugString(output->Begin());
  }
  else
  {
   String^ output = "Folder: " + item->Name + "\n";
   OutputDebugString(output->Begin());
  }
 }
});

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.

Method Create a shallow query that only returns items from the current folder Create 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) Default behavior of this method if none of the following options are specified.
- or -
Specify DefaultQuery as the value of CommonFileQuery or CommonFolderQuery when you instantiate the QueryOptions object.
- or -
Specify Shallow as the value of the FolderDepth property of the QueryOptions object.
For a library folder, specify a value other than DefaultQuery as the value of CommonFileQuery or CommonFolderQuery when you instantiate the QueryOptions object.
- or -
For any folder, specify Deep as the value of the FolderDepth property of the QueryOptions.

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

See also

Applies to

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:
 virtual IAsyncOperation<IVectorView<IStorageItem ^> ^> ^ GetItemsAsync(unsigned int startIndex, unsigned int maxItemsToRetrieve) = GetItemsAsync;
/// [Windows.Foundation.Metadata.Overload("GetItemsAsync")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<IVectorView<IStorageItem>> GetItemsAsync(uint32_t const& startIndex, uint32_t const& maxItemsToRetrieve);
[Windows.Foundation.Metadata.Overload("GetItemsAsync")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IReadOnlyList<IStorageItem>> GetItemsAsync(uint startIndex, uint maxItemsToRetrieve);
function getItemsAsync(startIndex, maxItemsToRetrieve)
Public Function GetItemsAsync (startIndex As UInteger, maxItemsToRetrieve As UInteger) As IAsyncOperation(Of IReadOnlyList(Of IStorageItem))

Parameters

startIndex
UInt32

unsigned int

uint32_t

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

maxItemsToRetrieve
UInt32

unsigned int

uint32_t

The maximum number of items to get.

Returns

When this method completes successfully, it returns a list of the files and subfolders in the current folder. The list is of type IReadOnlyList<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.

Implements

M:Windows.Storage.Search.IStorageFolderQueryOperations.GetItemsAsync(System.UInt32,System.UInt32) M:Windows.Storage.Search.IStorageFolderQueryOperations.GetItemsAsync(unsigned int,unsigned int) M:Windows.Storage.Search.IStorageFolderQueryOperations.GetItemsAsync(uint32_t,uint32_t)
Attributes

Exceptions

You don't have permission to access the contents of the current folder. For more information, see File access permissions.

See also

Applies to