Share via


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方法,以判斷每個專案都是檔案或資料夾。 然後將專案轉換成 StorageFolderStorageFile

實作

例外狀況

您沒有許可權存取目前資料夾的內容。

範例

下列範例示範如何藉由呼叫 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 方法。

適用於