StorageFolder.CreateFileQueryWithOptions(QueryOptions) 方法

定義

取得查詢結果物件,其中包含目前資料夾中的檔案,以及選擇性地在目前資料夾的子資料夾中。 結果是以指定的 QueryOptions為基礎。

public:
 virtual StorageFileQueryResult ^ CreateFileQueryWithOptions(QueryOptions ^ queryOptions) = CreateFileQueryWithOptions;
StorageFileQueryResult CreateFileQueryWithOptions(QueryOptions const& queryOptions);
public StorageFileQueryResult CreateFileQueryWithOptions(QueryOptions queryOptions);
function createFileQueryWithOptions(queryOptions)
Public Function CreateFileQueryWithOptions (queryOptions As QueryOptions) As StorageFileQueryResult

參數

queryOptions
QueryOptions

套用至查詢的準則。

傳回

查詢結果物件,其中包含目前資料夾中的檔案,並選擇性地在目前資料夾的子資料夾中,根據指定的 QueryOptions篩選和排序。 呼叫查詢結果的 GetFilesAsync 方法,以取得一般檔案清單,依 queryOptions所指定排序。 這個方法會傳回IReadOnlyList<StorageFile> 類型的清單。 每個檔案都是以 StorageFile類型的專案表示。

實作

例外狀況

您指定了非 Library 資料夾之CommonFileQuery列舉中的DefaultQuery以外的值。 檢查 查詢的值。

範例

下列範例示範如何藉由呼叫 CreateFileQueryWithOptions(QueryOptions) 方法來取得使用者 [圖片] 資料夾中的 JPG 檔案及其子資料夾中的排序方式。 此查詢是深層查詢,因為資料夾是程式庫資料夾,而且已指定CommonFileQuery列舉中的DefaultQuery以外的值。

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

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;

// Set options for file type and sort order.
List<string> fileTypeFilter = new List<string>();
fileTypeFilter.Add(".jpg");
QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderByDate, fileTypeFilter);

// Get the JPG files in the user's Pictures folder
// and its subfolders and sort them by date.
StorageFileQueryResult results = picturesFolder.CreateFileQueryWithOptions(queryOptions);

// Iterate over the results and print the list of files
// to the Visual Studio Output window.
IReadOnlyList<StorageFile> sortedFiles = await results.GetFilesAsync();
foreach (StorageFile item in sortedFiles)
{
    Debug.WriteLine(item.Name + ", " + item.DateCreated);
}
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() };

    // Set options for file type and sort order.
    Windows::Storage::Search::QueryOptions queryOptions{ Windows::Storage::Search::CommonFileQuery::OrderByDate, { L".png" } };

    // Get the png files in the user's Pictures folder and its subfolders, sorted by date.
    Windows::Storage::Search::StorageFileQueryResult results{ picturesFolder.CreateFileQueryWithOptions(queryOptions) };

    Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFile> filesInFolder{
        co_await results.GetFilesAsync() };

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

//Set options for file type and sort order
Platform::Collections::Vector<String^>^ fileTypeFilter = ref new Platform::Collections::Vector<String^>();
fileTypeFilter->Append(".jpg");
QueryOptions^ queryOptions = ref new QueryOptions(Windows::Storage::Search::CommonFileQuery::OrderByDate, fileTypeFilter);

//Get the JPG files in the user's pictures folder 
//and its subfolders and sort them by date
StorageFileQueryResult^ results = picturesFolder->CreateFileQueryWithOptions(queryOptions);

create_task(results->GetFilesAsync()).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());
    }
});

備註

在下列情況下,此查詢是淺層查詢,只會傳回目前資料夾中的檔案:

在下列情況下,此查詢是深層查詢,會從目前資料夾及其子資料夾傳回檔案:

提示

CommonFileQuery列舉中的部分值只能與程式庫資料夾搭配使用, (例如圖片庫) 或 Homegroup 資料夾。 雖然上述 ArgumentException 的字組,但除了具有非程式庫資料夾的資料夾之外,您還可以使用 OrderByName 和OrderBySearchRank選項) (。

如需識別淺層查詢和深層查詢的方法清單,請參閱 GetFilesAsync主題中的。

若要檢查您要指定的 QueryOptions 是否可用於目前資料夾,請呼叫資料夾的 AreQueryOptionsSupported 方法。 若要檢查特定 CommonFileQuery 是否可用,請呼叫資料夾的 IsCommonFileQuerySupported 方法。

您也可以呼叫其中一個 GetFilesAsync 方法,以非同步方式取得目前資料夾中的檔案清單。

若要取得包含目前資料夾中檔案的查詢結果物件,而不設定 QueryOptions 物件,請呼叫其中一個 CreateFileQuery 方法。

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

注意

針對Windows Server 2012,您必須安裝索引子元件以使用某些QueryOptions,因為預設不會安裝索引子元件。

適用於

另請參閱