Share via


StorageFolder.GetItemsAsync 方法

定义

重载

GetItemsAsync()

获取当前文件夹中的文件和子文件夹。

GetItemsAsync(UInt32, UInt32)

从当前文件夹中所有文件和文件夹的列表中获取基于索引的文件和文件夹范围。

GetItemsAsync()

获取当前文件夹中的文件和子文件夹。

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

返回

此方法成功完成后,将返回当前文件夹中文件和文件夹的列表。 列表的类型为 IReadOnlyList<IStorageItem>。 列表中的每个项都由 IStorageItem 对象表示。

若要处理返回的项目,请调用 IStorageItem 接口的 IsOfType 方法,以确定每个项是文件还是文件夹。 然后将项强制转换为 StorageFolderStorageFile

实现

属性

例外

您无权访问当前文件夹的内容。 有关详细信息,请参阅 文件访问权限

示例

以下示例演示如何通过调用 GetItemsAsync () 方法获取当前文件夹中的文件和子文件夹。

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

注解

下表列出了 StorageFolder 类中获取文件和文件夹列表的方法。 该表标识仅返回当前文件夹中的项目的浅表查询,以及从当前文件夹及其子文件夹中返回项目的深层查询。

若要从非库文件夹的文件夹获取深层查询结果,请调用 CreateItemQueryWithOptions (QueryOptions) 方法,并将 Deep 指定为 QueryOptions 对象的 FolderDepth 属性的值。

方法 创建仅返回当前文件夹中的项目的浅表查询 创建从当前文件夹及其子文件夹返回项的深层查询
GetItemsAsync () 此方法的默认行为。 不适用
GetItemsAsync (UInt32、UInt32) 此方法的默认行为。 不适用
CreateItemQuery () 此方法的默认行为。 不适用
CreateItemQueryWithOptions (QueryOptions) 如果未指定以下选项,则此方法的默认行为。
- 或 -
在实例化 QueryOptions 对象时,将 DefaultQuery 指定为 CommonFileQueryCommonFolderQuery 的值。
- 或 -
“浅层”指定为 QueryOptions 对象的 FolderDepth 属性的值。
对于库文件夹,在实例化 QueryOptions 对象时,将 DefaultQuery 以外的值指定为 CommonFileQueryCommonFolderQuery 的值。
- 或 -
对于任何文件夹,请指定 Deep 作为 QueryOptionsFolderDepth 属性的值。

若要仅获取文件,请调用 GetFilesAsync 方法。 若要仅获取文件夹,请调用 GetFoldersAsync 方法。

另请参阅

适用于

GetItemsAsync(UInt32, UInt32)

从当前文件夹中所有文件和文件夹的列表中获取基于索引的文件和文件夹范围。

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

参数

startIndex
UInt32

unsigned int

uint32_t

要获取的范围中第一项的从零开始的索引。

maxItemsToRetrieve
UInt32

unsigned int

uint32_t

要获取的最大项数。

返回

此方法成功完成后,将返回当前文件夹中的文件和子文件夹的列表。 列表的类型为 IReadOnlyList<IStorageItem>。 列表中的每个项都由 IStorageItem 对象表示。

若要处理返回的项目,请调用 IStorageItem 接口的 IsOfType 方法,以确定每个项是文件还是文件夹。 然后将项强制转换为 StorageFolderStorageFile

实现

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)
属性

例外

您无权访问当前文件夹的内容。 有关详细信息,请参阅 文件访问权限

另请参阅

适用于