StorageFolder.CreateItemQuery Método

Definición

Obtiene un objeto de resultado de consulta que contiene los archivos y subcarpetas de la carpeta actual.

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

Devoluciones

Objeto de resultado de la consulta. Llame al método GetItemsAsync del resultado de la consulta para obtener los archivos y subcarpetas de la carpeta actual. Este método devuelve una lista de tipo IReadOnlyList<IStorageItem>. Cada archivo o carpeta se representa mediante un elemento de tipo IStorageItem.

Para trabajar con los elementos devueltos, llame al método IsOfType de la interfaz IStorageItem para determinar si cada elemento es un archivo o una carpeta. A continuación, convierta el elemento en storageFolder o StorageFile.

Implementaciones

Excepciones

No tiene permiso para acceder al contenido de la carpeta actual.

Ejemplos

En el ejemplo siguiente se muestra cómo obtener los archivos y subcarpetas de la carpeta actual mediante una llamada al método 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());
        }        
    }
});

Comentarios

Esta consulta es una consulta superficial que devuelve solo los elementos de la carpeta actual. Para obtener una lista de métodos que identifican consultas poco profundas y consultas profundas, vea los comentarios del tema GetItemsAsync.

También puede obtener una lista de elementos de la carpeta actual de forma asincrónica llamando a uno de los métodos GetItemsAsync .

Para especificar opciones de consulta adicionales, llame al método CreateItemQueryWithOptions .

Se aplica a