StorageFolder.GetFilesAsync 方法

定義

多載

GetFilesAsync()

取得目前資料夾中的檔案。

GetFilesAsync(CommonFileQuery)

取得目前資料夾中的檔案。 當 查詢 引數的值不是 CommonFileQuery.DefaultQuery時,也會從目前資料夾的子資料夾中取得檔案。 檔案會根據 CommonFileQuery 列舉中的指定值來排序。

GetFilesAsync(CommonFileQuery, UInt32, UInt32)

從目前資料夾中所有檔案的清單取得以索引為基礎的檔案範圍。 當 查詢 引數的值不是 CommonFileQuery.DefaultQuery時,也會從目前資料夾的子資料夾中取得檔案。 檔案會根據 CommonFileQuery 列舉中的指定值來排序。

GetFilesAsync()

取得目前資料夾中的檔案。

public:
 virtual IAsyncOperation<IVectorView<StorageFile ^> ^> ^ GetFilesAsync() = GetFilesAsync;
/// [Windows.Foundation.Metadata.Overload("GetFilesAsyncOverloadDefaultOptionsStartAndCount")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<IVectorView<StorageFile>> GetFilesAsync();
[Windows.Foundation.Metadata.Overload("GetFilesAsyncOverloadDefaultOptionsStartAndCount")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IReadOnlyList<StorageFile>> GetFilesAsync();
function getFilesAsync()
Public Function GetFilesAsync () As IAsyncOperation(Of IReadOnlyList(Of StorageFile))

傳回

當此方法順利完成時,它會傳回目前資料夾中的檔案清單。 此清單的類型為IReadOnlyList<StorageFile> 。 清單中的每個檔案都是以 StorageFile 物件表示。

實作

屬性

例外狀況

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

範例

下列範例示範如何藉由呼叫 GetFilesAsync (CommonFileQuery、UInt32、UInt32、UInt32) 多載方法,取得使用者的 Pictures 資料夾及其子資料夾的內容。 這個範例會傳回最多 20 個檔案,從索引 0 的檔案開始。 由於 CommonFileQuery.OrderByDate 選項會依遞減順序排序日期 (,也就是從最新到最舊的) ,本範例會傳回使用者的 20 張最新相片。

執行下列範例之前,請在應用程式資訊清單檔案中啟用 圖片媒體櫃 功能。

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

// Get the user's Pictures folder.
// Enable the corresponding capability in the app manifest file.
StorageFolder picturesFolder = KnownFolders.PicturesLibrary;

// Get the first 20 files in the current folder, sorted by date.
IReadOnlyList<StorageFile> sortedItems = await picturesFolder.GetFilesAsync(CommonFileQuery.OrderByDate,0,20);

// Iterate over the results and print the list of files
// to the Visual Studio Output window.
foreach (StorageFile file in sortedItems)
    Debug.WriteLine(file.Name + ", " + file.DateCreated);
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Storage.Search.h>
...
IAsyncAction ExampleCoroutineAsync()
{
    // Get the user's Pictures library.
    // Enable the Pictures Library capability in the app manifest file.
    Windows::Storage::StorageFolder picturesLibrary{ Windows::Storage::KnownFolders::PicturesLibrary() };

    // Get the first 20 sorted images in the library, sorted by date.
    Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFile> filesInFolder{
        co_await picturesLibrary.GetFilesAsync(Windows::Storage::Search::CommonFileQuery::OrderByDate, 0, 20) };

    // Iterate over the results, and print the list of files to the Visual Studio output window.
    for (Windows::Storage::StorageFile const& fileInFolder : filesInFolder)
    {
        std::wstring output{ fileInFolder.Name() + L' ' };
        ::OutputDebugString(output.c_str());
    }
    ::OutputDebugString(L"\n");
}
// Get user's pictures library
StorageFolder^ picturesLibrary = KnownFolders::PicturesLibrary;

// Get the first 20 sorted images in the library
create_task(picturesLibrary->GetFilesAsync(CommonFileQuery::OrderByDate,0,20)).then([=](IVectorView<StorageFile^>^ filesInFolder) {
 //Iterate over the results and print the list of files
 // to the visual studio output window
 for (auto it = filesInFolder->First(); it->HasCurrent; it->MoveNext())
 {
  StorageFile^ file = it->Current;
  String^ output = file->Name + "\n";
  OutputDebugString(output->Begin());
 }
});

備註

此查詢是淺層查詢,只會傳回目前資料夾中的檔案。 如需識別淺層查詢和深層查詢的方法清單,請參閱 GetFilesAsync主題中的。

下表列出可取得檔案清單之 StorageFolder 類別的方法。 資料表會識別只會從目前資料夾傳回檔案的淺層查詢,以及從目前資料夾和其子資料夾傳回檔案的深層查詢。

某些方法會從 CommonFileQuery 列舉取得值。 當您從CommonFileQuery列舉指定DefaultQuery選項時,查詢是一個淺層查詢,只會傳回目前資料夾中的檔案。 當您從 CommonFileQuery 列舉指定另一個值時,查詢是深層查詢,會從目前資料夾和其子資料夾傳回檔案的扁平化清單。

提示

CommonFileQuery列舉的某些值只能與文件庫資料夾搭配使用, (例如圖片庫) 或 Homegroup 資料夾。 除了 DefaultQuery 選項之外,您只能使用 OrderByNameOrderBySearchRank 選項搭配非程式庫資料夾的資料夾。

若要從不是程式庫資料夾的資料夾取得深層查詢結果,請呼叫CreateFileQueryWithOptions (QueryOptions) 方法,並將Deep指定為QueryOptions物件的FolderDepth屬性值。

方法 建立僅從目前資料夾傳回檔案的淺層查詢 建立深層查詢,以從目前資料夾和其子資料夾傳回檔案
GetFilesAsync () 這個方法的預設行為。 N/A
GetFilesAsync (CommonFileQuery) 指定 DefaultQuery 選項。 針對程式庫資料夾,指定 DefaultQuery以外的選項。
GetFilesAsync (CommonFileQuery, UInt32, UInt32) 指定 DefaultQuery 選項。 針對程式庫資料夾,指定 DefaultQuery以外的選項。
CreateFileQuery () 這個方法的預設行為。 N/A
CreateFileQuery (CommonFileQuery) 指定 DefaultQuery 選項。 針對程式庫資料夾,指定 DefaultQuery以外的選項。
CreateFileQueryWithOptions (QueryOptions) 如果未指定下列任何選項,這個方法的預設行為。
- 或 -
當您具現化QueryOptions物件時,請將DefaultQuery指定為CommonFileQuery的值。
- 或 -
[淺層] 指定為QueryOptions物件的FolderDepth屬性值。
對於程式庫資料夾,當您具現化QueryOptions物件時,請將DefaultQuery以外的值指定為CommonFileQuery的值。
- 或 -
針對任何資料夾,將Deep指定為QueryOptionsFolderDepth屬性值。

另請參閱

適用於

GetFilesAsync(CommonFileQuery)

取得目前資料夾中的檔案。 當 查詢 引數的值不是 CommonFileQuery.DefaultQuery時,也會從目前資料夾的子資料夾中取得檔案。 檔案會根據 CommonFileQuery 列舉中的指定值來排序。

public:
 virtual IAsyncOperation<IVectorView<StorageFile ^> ^> ^ GetFilesAsync(CommonFileQuery query) = GetFilesAsync;
/// [Windows.Foundation.Metadata.Overload("GetFilesAsyncOverloadDefaultStartAndCount")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<IVectorView<StorageFile>> GetFilesAsync(CommonFileQuery const& query);
[Windows.Foundation.Metadata.Overload("GetFilesAsyncOverloadDefaultStartAndCount")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IReadOnlyList<StorageFile>> GetFilesAsync(CommonFileQuery query);
function getFilesAsync(query)
Public Function GetFilesAsync (query As CommonFileQuery) As IAsyncOperation(Of IReadOnlyList(Of StorageFile))

參數

query
CommonFileQuery

其中一個列舉值,指定如何排序檔案,並判斷查詢是否為淺層或深層。

傳回

當這個方法順利完成時,它會傳回一般檔案清單,依 查詢所指定排序。 此清單的類型為IReadOnlyList<StorageFile> 。 清單中的每個檔案都是以 StorageFile 物件表示。

實作

屬性

例外狀況

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

另請參閱

適用於

GetFilesAsync(CommonFileQuery, UInt32, UInt32)

從目前資料夾中所有檔案的清單取得以索引為基礎的檔案範圍。 當 查詢 引數的值不是 CommonFileQuery.DefaultQuery時,也會從目前資料夾的子資料夾中取得檔案。 檔案會根據 CommonFileQuery 列舉中的指定值來排序。

public:
 virtual IAsyncOperation<IVectorView<StorageFile ^> ^> ^ GetFilesAsync(CommonFileQuery query, unsigned int startIndex, unsigned int maxItemsToRetrieve) = GetFilesAsync;
/// [Windows.Foundation.Metadata.Overload("GetFilesAsync")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<IVectorView<StorageFile>> GetFilesAsync(CommonFileQuery const& query, uint32_t const& startIndex, uint32_t const& maxItemsToRetrieve);
[Windows.Foundation.Metadata.Overload("GetFilesAsync")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IReadOnlyList<StorageFile>> GetFilesAsync(CommonFileQuery query, uint startIndex, uint maxItemsToRetrieve);
function getFilesAsync(query, startIndex, maxItemsToRetrieve)
Public Function GetFilesAsync (query As CommonFileQuery, startIndex As UInteger, maxItemsToRetrieve As UInteger) As IAsyncOperation(Of IReadOnlyList(Of StorageFile))

參數

query
CommonFileQuery

其中一個列舉值,指定如何排序檔案,並判斷查詢是否為淺層或深層。

startIndex
UInt32

unsigned int

uint32_t

要擷取之範圍中第一個檔案之以零起始的索引。

maxItemsToRetrieve
UInt32

unsigned int

uint32_t

要擷取的檔案數目上限。

傳回

當此方法成功完成時,它會傳回依 查詢所指定排序的一般檔案清單。 此清單的類型為IReadOnlyList<StorageFile> 。 清單中的每個檔案都是以 StorageFile 物件表示。

實作

M:Windows.Storage.Search.IStorageFolderQueryOperations.GetFilesAsync(Windows.Storage.Search.CommonFileQuery,System.UInt32,System.UInt32) M:Windows.Storage.Search.IStorageFolderQueryOperations.GetFilesAsync(Windows.Storage.Search.CommonFileQuery,unsigned int,unsigned int) M:Windows.Storage.Search.IStorageFolderQueryOperations.GetFilesAsync(Windows.Storage.Search.CommonFileQuery,uint32_t,uint32_t)
屬性

例外狀況

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

另請參閱

適用於