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 型の項目で表されます。

実装

例外

ライブラリ フォルダーではないフォルダーに対して、CommonFileQuery 列挙から DefaultQuery 以外の値を指定しました。 クエリの値を確認 します

次の例は、 メソッドを呼び出して、ユーザーの Pictures フォルダーとそのサブフォルダー内の JPG ファイルを日付順に並べ替える方法を CreateFileQueryWithOptions(QueryOptions) 示しています。 フォルダーがライブラリ フォルダーであり、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());
    }
});

注釈

次の場合、このクエリは、現在のフォルダー内のファイルのみを返す浅いクエリです。

  • 次のオプションが指定されていない場合の、このメソッドの既定の動作。 または:
  • QueryOptions オブジェクトをインスタンス化するときに、CommonFileQuery の値として DefaultQuery を指定します。 または:
  • QueryOptions オブジェクトの FolderDepth プロパティの値として、Shallow を指定します。

次の場合、このクエリは、現在のフォルダーとそのサブフォルダーからファイルを返すディープ クエリです。

  • ライブラリ フォルダーの場合は、QueryOptions オブジェクトをインスタンス化するときに、DefaultQuery 以外の値を CommonFileQuery の値として指定します。 または:
  • 任意のフォルダーに対して、QueryOptionsFolderDepth プロパティの値として Deep を指定します。

ヒント

CommonFileQuery 列挙の値の一部は、ライブラリ フォルダー (Pictures ライブラリなど) または Homegroup フォルダーでのみ使用できます。 前述の ArgumentException の文言にもかかわらず、OrderByName オプションと OrderBySearchRank オプション (DefaultQuery オプションに加えて) をライブラリ フォルダーではないフォルダーと共に使用できます。

シャロー クエリとディープ クエリを識別するメソッドの一覧については、 GetFilesAsync トピックの「解説」を参照してください。

指定する QueryOptions が現在のフォルダーで使用できるかどうかを確認するには、フォルダーの AreQueryOptionsSupported メソッドを呼び出します。 特定の CommonFileQuery が使用可能かどうかを確認するには、フォルダーの IsCommonFileQuerySupported メソッドを呼び出します。

GetFilesAsync メソッドのいずれかを呼び出すことで、現在のフォルダー内のファイルの一覧を非同期的に取得することもできます。

QueryOptions オブジェクトを構成せずに、現在のフォルダー内のファイルを含むクエリ結果オブジェクトを取得するには、CreateFileQuery メソッドのいずれかを呼び出します。

ファイルまたはフォルダーであるアイテムを取得するには、 CreateItemQueryWithOptions メソッドを 呼び出します。

Note

Windows Server 2012の場合、インデクサー コンポーネントは既定ではインストールされないため、一部の QueryOptions を使用するにはインデクサー コンポーネントをインストールする必要があります。

適用対象

こちらもご覧ください