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 () 這個方法的預設行為。 N/A
GetItemsAsync (UInt32、 UInt32) 這個方法的預設行為。 N/A
CreateItemQuery () 這個方法的預設行為。 N/A
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)
屬性

例外狀況

您沒有許可權存取目前資料夾的內容。 如需詳細資訊,請參閱 檔案存取權限

另請參閱

適用於