StorageFolder.GetFoldersAsync 方法

定義

多載

GetFoldersAsync()

取得目前資料夾中的子資料夾。

GetFoldersAsync(CommonFolderQuery)

取得目前資料夾中的子資料夾。 當 查詢 引數的值不是 CommonFolderQuery.DefaultQuery時,會取得虛擬資料夾清單,代表目前資料夾子資料夾中檔案群組的容器。 檔案會根據 CommonFolderQuery 列舉中的指定值,分組到資料夾中。

GetFoldersAsync(CommonFolderQuery, UInt32, UInt32)

從目前資料夾中的所有子資料夾清單中,取得資料夾的索引型範圍。 當 查詢 引數的值不是 CommonFolderQuery.DefaultQuery時,會取得虛擬資料夾清單,代表目前資料夾子資料夾中檔案群組的容器。 檔案會根據 CommonFolderQuery 列舉中的指定值,分組到資料夾中。

GetFoldersAsync()

取得目前資料夾中的子資料夾。

public:
 virtual IAsyncOperation<IVectorView<StorageFolder ^> ^> ^ GetFoldersAsync() = GetFoldersAsync;
/// [Windows.Foundation.Metadata.Overload("GetFoldersAsyncOverloadDefaultOptionsStartAndCount")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<IVectorView<StorageFolder>> GetFoldersAsync();
[Windows.Foundation.Metadata.Overload("GetFoldersAsyncOverloadDefaultOptionsStartAndCount")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IReadOnlyList<StorageFolder>> GetFoldersAsync();
function getFoldersAsync()
Public Function GetFoldersAsync () As IAsyncOperation(Of IReadOnlyList(Of StorageFolder))

傳回

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

實作

屬性

例外狀況

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

範例

下列範例示範如何藉由呼叫 GetFoldersAsync (CommonFolderQuery、UInt32、UInt32、UInt32、UInt32) 方法來取得使用者 [圖片] 資料夾中的子資料夾內容。 (目前資料夾根目錄中的檔案不包含。) 本範例會傳回最多 4 個資料夾,從索引 0 的資料夾開始。 由於 CommonFolderQuery.GroupByMonth 選項會以遞減順序排序日期 (也就是說,從最新到最舊的) ,本範例會傳回使用者有相片的 4 個月資料夾。 每個資料夾都包含該月份中的所有使用者相片。

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

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 files in the subfolders of the user's Pictures folder,
// grouped by month. Get only the first 4 folders (months).
IReadOnlyList <StorageFolder> groupedItems = await picturesFolder.GetFoldersAsync(CommonFolderQuery.GroupByMonth, 0, 4);

// Iterate over the results and print the list of folders
// and files to the Visual Studio Output window.
foreach (StorageFolder folder in groupedItems)
{
    Debug.WriteLine(folder.Name);

    // To iterate over the files in each folder, uncomment the following lines. 
    // foreach(StorageFile file in await folder.GetFilesAsync())
    //    Debug.WriteLine(" " + file.Name);
}
IAsyncAction MainPage::ExampleCoroutineAsync()
{
    // Get the users's Pictures folder.
    // Enable the Pictures Library capability in the app manifest file.
    Windows::Storage::StorageFolder picturesFolder{ Windows::Storage::KnownFolders::PicturesLibrary() };

    // Get the files in the user's Pictures folder, grouped by month.
    // Get only the first 4 folders (months).
    Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFolder> itemsInFolder{
        co_await picturesFolder.GetFoldersAsync(Windows::Storage::Search::CommonFolderQuery::GroupByMonth, 0, 4) };

    // Iterate over the results, and print the list of file groups to the Visual Studio output window.
    for (StorageFolder const& itemInFolder : itemsInFolder)
    {
        std::wstring output{ itemInFolder.Name() };
        ::OutputDebugString(output.c_str());
    }
}
// Get the user's Pictures folder.
// Enable the corresponding capability in the app manifest file.
StorageFolder^ picturesFolder = KnownFolders::PicturesLibrary;

// Get the files in the user's Pictures folder, grouped by month.
// Get only the first 4 folders (months).
create_task(picturesFolder->GetFoldersAsync(CommonFolderQuery::GroupByMonth, 0, 4)).then([=](IVectorView<StorageFolder^>^ itemsInFolder) {
 //Iterate over the results and print the list of file groups
 // to the visual studio output window
 for (auto it = itemsInFolder->First(); it->HasCurrent; it->MoveNext())
 {
  StorageFolder^ file = it->Current;
  String^ output = file->Name + "\n";
  OutputDebugString(output->Begin());
 }
});

備註

此查詢是淺層查詢,只會傳回目前資料夾中的子資料夾。

下表列出可取得子資料夾清單之 StorageFolder 類別的方法。 資料表會識別只從目前資料夾傳回子資料夾的淺層查詢,以及傳回巢狀子資料夾內容的深層查詢,並分組到虛擬資料夾。

某些方法會從 CommonFolderQuery 列舉取得值。

  • 當您搭配任何資料夾使用 DefaultQuery 選項時,查詢會傳回檔案系統中的子資料夾清單。
  • 當您使用 DefaultQuery 以外的選項搭配程式庫資料夾時,查詢會傳回虛擬資料夾清單,代表目前資料夾子資料夾中檔案的容器。 (不包含目前資料夾中的檔案。) 檔案會根據 CommonFolderQuery 列舉中的指定值,將檔案分組到虛擬資料夾中。 例如,如果您指定 GroupByMonth,查詢會傳回虛擬資料夾的清單,例如 July 2014August 2014September 2014

提示

您可以使用 DefaultQuery 選項搭配任何資料夾;您可以使用 CommonFolderQuery 列舉中的其他選項搭配媒體櫃資料夾,例如圖片媒體櫃或 Homegroup 資料夾。

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

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

另請參閱

適用於

GetFoldersAsync(CommonFolderQuery)

取得目前資料夾中的子資料夾。 當 查詢 引數的值不是 CommonFolderQuery.DefaultQuery時,會取得虛擬資料夾清單,代表目前資料夾子資料夾中檔案群組的容器。 檔案會根據 CommonFolderQuery 列舉中的指定值,分組到資料夾中。

public:
 virtual IAsyncOperation<IVectorView<StorageFolder ^> ^> ^ GetFoldersAsync(CommonFolderQuery query) = GetFoldersAsync;
/// [Windows.Foundation.Metadata.Overload("GetFoldersAsyncOverloadDefaultStartAndCount")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<IVectorView<StorageFolder>> GetFoldersAsync(CommonFolderQuery const& query);
[Windows.Foundation.Metadata.Overload("GetFoldersAsyncOverloadDefaultStartAndCount")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IReadOnlyList<StorageFolder>> GetFoldersAsync(CommonFolderQuery query);
function getFoldersAsync(query)
Public Function GetFoldersAsync (query As CommonFolderQuery) As IAsyncOperation(Of IReadOnlyList(Of StorageFolder))

參數

query
CommonFolderQuery

其中一個列舉值,指定如何將檔案分組至資料夾,並判斷查詢是否為淺層或深層。

傳回

當此方法順利完成時,它會傳回子資料夾的清單。 當 查詢 引數的值不是 CommonFolderQuery.DefaultQuery時,這個方法會傳回虛擬資料夾清單,代表目前資料夾子資料夾中檔案群組的容器。 (不包含目前資料夾中的檔案。) 檔案會依 查詢所指定分組。 此清單的類型為IReadOnlyList<StorageFolder> 。 清單中的每一個資料夾都是以 StorageFolder 物件表示。

實作

屬性

例外狀況

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

另請參閱

適用於

GetFoldersAsync(CommonFolderQuery, UInt32, UInt32)

從目前資料夾中的所有子資料夾清單中,取得資料夾的索引型範圍。 當 查詢 引數的值不是 CommonFolderQuery.DefaultQuery時,會取得虛擬資料夾清單,代表目前資料夾子資料夾中檔案群組的容器。 檔案會根據 CommonFolderQuery 列舉中的指定值,分組到資料夾中。

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

參數

query
CommonFolderQuery

其中一個列舉值,指定如何將檔案分組至資料夾,並判斷查詢是否為淺層或深層。

startIndex
UInt32

unsigned int

uint32_t

要擷取之範圍中第一個資料夾之以零起始的索引。

maxItemsToRetrieve
UInt32

unsigned int

uint32_t

要擷取的資料夾數目上限。

傳回

當此方法順利完成時,它會傳回子資料夾的清單。 當 查詢 引數的值不是 CommonFolderQuery.DefaultQuery時,這個方法會傳回虛擬資料夾清單,代表目前資料夾子資料夾中檔案群組的容器。 (不包含目前資料夾中的檔案。) 檔案會依 查詢所指定分組。 此清單的類型為IReadOnlyList<StorageFolder> 。 清單中的每一個資料夾都是以 StorageFolder 物件表示。

實作

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

例外狀況

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

另請參閱

適用於