Share via


StorageFolder.CreateFolderQuery 方法

定義

多載

CreateFolderQuery()

取得查詢結果物件,其中包含目前資料夾中的子資料夾。

CreateFolderQuery(CommonFolderQuery)

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

CreateFolderQuery()

取得查詢結果物件,其中包含目前資料夾中的子資料夾。

public:
 virtual StorageFolderQueryResult ^ CreateFolderQuery() = CreateFolderQuery;
/// [Windows.Foundation.Metadata.Overload("CreateFolderQueryOverloadDefault")]
StorageFolderQueryResult CreateFolderQuery();
[Windows.Foundation.Metadata.Overload("CreateFolderQueryOverloadDefault")]
public StorageFolderQueryResult CreateFolderQuery();
function createFolderQuery()
Public Function CreateFolderQuery () As StorageFolderQueryResult

傳回

查詢結果物件。 呼叫查詢結果的 GetFoldersAsync 方法,以取得目前資料夾中的子資料夾。 這個方法會傳回IReadOnlyList<StorageFolder> 類型的清單。 每個檔案或資料夾都是以 StorageFolder類型的專案表示。

實作

屬性

例外狀況

您沒有存取目前資料夾內容的許可權。

範例

下列範例示範如何藉由呼叫 GetFoldersAsync (CommonFolderQuery) 多載方法,取得使用者 [圖片] 資料夾中子資料夾的內容,並依月份分組為資料夾。 (目前資料夾根目錄中的檔案不包含。)

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

using Windows.Storage;
using Windows.Storage.Search;
using System.Threading.Tasks;
using System.Diagnostics; // For writing results to the 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.
StorageFolderQueryResult groupedItems =
    picturesFolder.CreateFolderQuery(CommonFolderQuery.GroupByMonth);

// Iterate over the results and print the list of folders
// and files to the Visual Studio Output window.
foreach (StorageFolder folder in await groupedItems.GetFoldersAsync())
{
    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, and group them by month.
    Windows::Storage::Search::StorageFolderQueryResult results{ picturesFolder.CreateFolderQuery(Windows::Storage::Search::CommonFolderQuery::GroupByMonth) };

    Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFolder> itemsInFolder{
        co_await results.GetFoldersAsync() };

    // 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 user's pictures folder
StorageFolder^ picturesFolder = KnownFolders::PicturesLibrary;

// Get the files in the user's Pictures folder and group them by month
StorageFolderQueryResult^ itemsInFolder = picturesFolder->CreateFolderQuery(CommonFolderQuery::GroupByMonth);

create_task(itemsInFolder->GetFoldersAsync()).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());
 }
});

備註

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

若要指定其他查詢選項,請呼叫 CreateFolderQueryWithOptions 方法。

若要取得檔案或資料夾的專案,請呼叫 CreateItemQuery 方法。

另請參閱

適用於

CreateFolderQuery(CommonFolderQuery)

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

public:
 virtual StorageFolderQueryResult ^ CreateFolderQuery(CommonFolderQuery query) = CreateFolderQuery;
/// [Windows.Foundation.Metadata.Overload("CreateFolderQuery")]
StorageFolderQueryResult CreateFolderQuery(CommonFolderQuery const& query);
[Windows.Foundation.Metadata.Overload("CreateFolderQuery")]
public StorageFolderQueryResult CreateFolderQuery(CommonFolderQuery query);
function createFolderQuery(query)
Public Function CreateFolderQuery (query As CommonFolderQuery) As StorageFolderQueryResult

參數

query
CommonFolderQuery

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

傳回

查詢結果物件。 呼叫查詢結果的 GetFoldersAsync 方法,以取得目前資料夾中的子資料夾。 當 query 引數的值不是 CommonFolderQuery.DefaultQuery時,查詢結果物件會包含虛擬資料夾清單,代表目前資料夾子資料夾中檔案群組的容器。 (目前資料夾中的檔案不包含。) 檔案會依 查詢指定分組。 此清單的類型為IReadOnlyList<StorageFolder> 。 清單中的每個資料夾都會以 StorageFolder 物件表示。

實作

屬性

例外狀況

您沒有存取目前資料夾內容的許可權。

您指定了 xref:Windows.Storage.Search.CommonFolderQuery?text=CommonFolderQuery > 列舉中不是程式庫資料夾之資料夾的DefaultQuery< 以外的值。 檢查 查詢的值。

備註

CommonFolderQuery會根據特定檔案屬性,將子資料夾的內容分組到資料夾中, (例如作者或相簿) 快速且輕鬆地。 如需指定DefaultQuery選項的詳細資訊,請參閱CreateFileQuery方法頁面上的。

另請參閱

適用於