StorageFolder.CreateFileQuery 方法

定義

多載

CreateFileQuery()

取得查詢結果物件,其中包含目前資料夾中的檔案。

CreateFileQuery(CommonFileQuery)

取得查詢結果物件,其中包含目前資料夾中的檔案。 當 查詢 引數的值不是 CommonFileQuery.DefaultQuery時,也會從目前資料夾的子資料夾中取得檔案。 檔案會根據指定的 CommonFileQuery排序。

CreateFileQuery()

取得查詢結果物件,其中包含目前資料夾中的檔案。

public:
 virtual StorageFileQueryResult ^ CreateFileQuery() = CreateFileQuery;
/// [Windows.Foundation.Metadata.Overload("CreateFileQueryOverloadDefault")]
StorageFileQueryResult CreateFileQuery();
[Windows.Foundation.Metadata.Overload("CreateFileQueryOverloadDefault")]
public StorageFileQueryResult CreateFileQuery();
function createFileQuery()
Public Function CreateFileQuery () As StorageFileQueryResult

傳回

查詢結果物件。 呼叫查詢結果的 GetFilesAsync 方法,以取得檔案的一般清單。 這個方法會傳回IReadOnlyList<StorageFile> 類型的清單。 每個檔案都是以 StorageFile類型的專案來表示。

實作

屬性

範例

下列範例會藉由呼叫 CreateFileQuery () 方法,取得包含目前資料夾中檔案的查詢結果物件。

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

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

// Get the files in the current folder.
StorageFileQueryResult results = appFolder.CreateFileQuery();

// Iterate over the results and print the list of files
// to the Visual Studio Output window.
IReadOnlyList<StorageFile> filesInFolder = await results.GetFilesAsync();
foreach (StorageFile item in filesInFolder)
{
    Debug.WriteLine(item.Name);
}
IAsyncAction MainPage::ExampleCoroutineAsync()
{
    // Get the app's installation folder.
    Windows::Storage::StorageFolder appFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };
    // Get the files in the current folder.
    Windows::Storage::Search::StorageFileQueryResult results{ appFolder.CreateFileQuery() };

    Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFile> filesInFolder{
        co_await results.GetFilesAsync() };

    // Iterate over the results, and print the list of files to the Visual Studio output window.
    for (StorageFile const& fileInFolder : filesInFolder)
    {
        std::wstring output{ fileInFolder.Name() };
        ::OutputDebugString(output.c_str());
    }
}
//Get the app's installation folder
StorageFolder^ appFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;
//Get the files in the current folder
StorageFileQueryResult^ results = appFolder->CreateFileQuery();

create_task(results->GetFilesAsync()).then([=](IVectorView<StorageFile^>^ filesInFolder) {
 //Iterate over the results and print the list of files
 // to the visual studio output window
 for (auto it = filesInFolder->First(); it->HasCurrent; it->MoveNext())
 {
  StorageFile^ file = it->Current;
  String^ output = file->Name + "\n";
  OutputDebugString(output->Begin());
 }
});

備註

此查詢是淺層查詢,只會傳回目前資料夾中的檔案。 如需識別淺層查詢和深層查詢的方法清單,請參閱 GetFilesAsync主題中的。

您也可以呼叫其中一個 GetFilesAsync 方法,以非同步方式取得目前資料夾中的檔案清單。

若要指定其他查詢選項,請呼叫 CreateFileQueryWithOptions 方法。

若要取得檔案或資料夾的專案,請呼叫 CreateItemQuery 方法。

另請參閱

適用於

CreateFileQuery(CommonFileQuery)

取得查詢結果物件,其中包含目前資料夾中的檔案。 當 查詢 引數的值不是 CommonFileQuery.DefaultQuery時,也會從目前資料夾的子資料夾中取得檔案。 檔案會根據指定的 CommonFileQuery排序。

public:
 virtual StorageFileQueryResult ^ CreateFileQuery(CommonFileQuery query) = CreateFileQuery;
/// [Windows.Foundation.Metadata.Overload("CreateFileQuery")]
StorageFileQueryResult CreateFileQuery(CommonFileQuery const& query);
[Windows.Foundation.Metadata.Overload("CreateFileQuery")]
public StorageFileQueryResult CreateFileQuery(CommonFileQuery query);
function createFileQuery(query)
Public Function CreateFileQuery (query As CommonFileQuery) As StorageFileQueryResult

參數

query
CommonFileQuery

其中一個列舉值,指定如何排序檔案,並判斷查詢是否為淺層或深層。

傳回

查詢結果物件。 呼叫查詢結果的 GetFilesAsync 方法,以取得一般檔案清單,依 查詢所指定排序。 這個方法會傳回IReadOnlyList<StorageFile> 類型的清單。 每個檔案都是以 StorageFile類型的專案來表示。

實作

屬性

例外狀況

您為 < 不是程式庫資料夾的資料夾指定 xref:Windows.Storage.Search.CommonFileQuery?text=CommonFileQuery 列舉以外的DefaultQuery> 值。 檢查 查詢的值。

備註

CommonFileQuery會根據特定檔案屬性來排序檔案, (例如標題或日期) 快速且輕鬆地排序。

當您從CommonFileQuery列舉指定DefaultQuery選項時,此查詢是只會傳回目前資料夾中檔案的淺層查詢。 當您從 CommonFileQuery 列舉指定另一個值時,此查詢是深層查詢,會從目前資料夾和其子資料夾傳回檔案的扁平化清單。

提示

CommonFileQuery列舉的某些值只能與文件庫資料夾搭配使用, (例如圖片庫) 或 Homegroup 資料夾。 除了 DefaultQuery 選項之外,您只能使用 OrderByNameOrderBySearchRank 選項搭配非程式庫資料夾的資料夾。

如需識別淺層查詢和深層查詢的方法清單,請參閱 GetFilesAsync主題中的。

若要檢查特定 CommonFileQuery 是否可用,請呼叫資料夾的 IsCommonFileQuerySupported 方法。

您也可以呼叫其中一個 GetFilesAsync 方法,以非同步方式取得目前資料夾中的檔案清單。

針對 Windows Server 2012 您必須安裝索引子元件,才能使用一些 CommonFileQuery 值,因為預設不會安裝索引子元件。

另請參閱

適用於