StorageFolder.CreateItemQuery Metode

Definisi

Mendapatkan objek hasil kueri yang berisi file dan subfolder di folder saat ini.

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

Mengembalikan

Objek hasil kueri. Panggil metode GetItemsAsync dari hasil kueri untuk mendapatkan file dan subfolder di folder saat ini. Metode ini mengembalikan daftar jenis IReadOnlyList<IStorageItem>. Setiap file atau folder diwakili oleh item jenis IStorageItem.

Untuk bekerja dengan item yang dikembalikan, panggil metode IsOfType antarmuka IStorageItem untuk menentukan apakah setiap item adalah file atau folder. Kemudian transmisikan item ke StorageFolder atau StorageFile.

Penerapan

Pengecualian

Anda tidak memiliki izin untuk mengakses isi folder saat ini.

Contoh

Contoh berikut menunjukkan cara mendapatkan file dan subfolder di folder saat ini dengan memanggil metode 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());
        }        
    }
});

Keterangan

Kueri ini adalah kueri dangkal yang hanya mengembalikan item di folder saat ini. Untuk daftar metode yang mengidentifikasi kueri dangkal dan kueri mendalam, lihat Keterangan dalam topik GetItemsAsync.

Anda juga bisa mendapatkan daftar item di folder saat ini secara asinkron dengan memanggil salah satu metode GetItemsAsync .

Untuk menentukan opsi kueri tambahan, panggil metode CreateItemQueryWithOptions .

Berlaku untuk