StorageFolder.CreateItemQuery メソッド

定義

現在のフォルダー内のファイルとサブフォルダーを含むクエリ結果オブジェクトを取得します。

public:
 virtual StorageItemQueryResult ^ CreateItemQuery() = CreateItemQuery;
StorageItemQueryResult CreateItemQuery();
public StorageItemQueryResult CreateItemQuery();
function createItemQuery()
Public Function CreateItemQuery () As StorageItemQueryResult

戻り値

クエリ結果オブジェクト。 クエリ結果の GetItemsAsync メソッドを呼び出して、現在のフォルダー内のファイルとサブフォルダーを取得します。 このメソッドは、IReadOnlyList<IStorageItem 型のリストを返します>。 各ファイルまたはフォルダーは、 IStorageItem 型の項目で表されます。

返されたアイテムを操作するには、IStorageItem インターフェイスの IsOfType メソッドを呼び出して、各項目がファイルかフォルダーかを判断します。 次に、アイテムを StorageFolder または StorageFile にキャストします。

実装

例外

現在のフォルダーの内容にアクセスするためのアクセス許可がありません。

次の例では、CreateItemQuery() メソッドを呼び出して、現在のフォルダー内のファイルとサブフォルダーを取得する方法を示します。

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

// Get the app's installation folder.
StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;

// Get the items in the current folder.
StorageItemQueryResult itemsInFolder = appFolder.CreateItemQuery();

// Iterate over the results and print the list of items
// to the Visual Studio Output window.
foreach (IStorageItem item in await itemsInFolder.GetItemsAsync())
{
    if(item.IsOfType(StorageItemTypes.Folder))
        Debug.WriteLine("Folder: " + item.Name);
    else
        Debug.WriteLine("File: " + item.Name + ", " + item.DateCreated);
}
IAsyncAction MainPage::ExampleCoroutineAsync()
{
    // Get the app's installation folder.
    Windows::Storage::StorageFolder appFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };

    Windows::Storage::Search::StorageItemQueryResult results{ appFolder.CreateItemQuery() };

    // Get the items in the current folder.
    Windows::Foundation::Collections::IVectorView<Windows::Storage::IStorageItem> itemsInFolder{
        co_await results.GetItemsAsync() };

    // Iterate over the results, and print the list of items to the Visual Studio output window.
    for (IStorageItem const& itemInFolder : itemsInFolder)
    {
        std::wstringstream stringstream;

        if (itemInFolder.IsOfType(Windows::Storage::StorageItemTypes::File))
        {
            stringstream << L"File: ";
        }
        else
        {
            stringstream << L"Folder: ";
        }

        stringstream << itemInFolder.Name().c_str() << std::endl;
        ::OutputDebugString(stringstream.str().c_str());
    }
}
// Get the app's installation folder
StorageFolder^ appFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;

StorageItemQueryResult^ results = appFolder->CreateItemQuery();

// Get the items in the current folder; 
create_task(results->GetItemsAsync()).then([=](IVectorView<IStorageItem^>^ itemsInFolder) {

 //Iterate over the results and print the list of items
    // to the visual studio output window
    for (auto it = itemsInFolder->First(); it->HasCurrent; it->MoveNext())
    {
        IStorageItem^ item = it->Current;
        if (item->IsOfType(StorageItemTypes::File))
        {
            String^ output = "File: " + item->Name + "\n";
            OutputDebugString(output->Begin());
        }
        else
        {
            String^ output = "Folder: " + item->Name + "\n";
            OutputDebugString(output->Begin());
        }        
    }
});

注釈

このクエリは、現在のフォルダー内のアイテムのみを返す浅いクエリです。 シャロー クエリとディープ クエリを識別するメソッドの一覧については、 GetItemsAsync トピックの「解説」を参照してください。

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

追加のクエリ オプションを指定するには、 CreateItemQueryWithOptions メソッドを 呼び出します。

適用対象