StorageFolder.CreateItemQuery Метод

Определение

Возвращает объект результата запроса, содержащий файлы и вложенные папки в текущей папке.

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

Возвращаемое значение

Объект результата запроса. Вызовите метод GetItemsAsync результата запроса, чтобы получить файлы и вложенные папки в текущей папке. Этот метод возвращает список типа IReadOnlyList<IStorageItem>. Каждый файл или папка представлена элементом типа IStorageItem.

Для работы с возвращаемыми элементами вызовите метод IsOfType интерфейса IStorageItem , чтобы определить, является ли каждый элемент файлом или папкой. Затем приведите элемент к 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 .

Применяется к