Delen via


StorageFolder.CreateFileQuery Method

Definition

Overloads

CreateFileQuery()

Gets a query result object that contains the files in the current folder.

CreateFileQuery(CommonFileQuery)

Gets a query result object that contains the files in the current folder. Also gets the files from the subfolders of the current folder when the value of the query argument is something other than CommonFileQuery.DefaultQuery. Files are sorted based on the specified CommonFileQuery.

CreateFileQuery()

Gets a query result object that contains the files in the current folder.

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

Returns

A query result object. Call the GetFilesAsync method of the query result to get the flat list of files. This method returns a list of type IReadOnlyList<StorageFile>. Each file is represented by an item of type StorageFile.

Implements

Attributes

Examples

The following example gets a query result object that contains the files in the current folder by calling the CreateFileQuery() method.

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());
 }
});

Remarks

This query is a shallow query that returns only files in the current folder. For a list of methods that identifies shallow queries and deep queries, see the Remarks in the topic GetFilesAsync.

You can also get a list of files in the current folder asynchronously by calling one of the GetFilesAsync methods.

To specify additional query options, call the CreateFileQueryWithOptions method.

To get items that are files or folders, call the CreateItemQuery method.

See also

Applies to

CreateFileQuery(CommonFileQuery)

Gets a query result object that contains the files in the current folder. Also gets the files from the subfolders of the current folder when the value of the query argument is something other than CommonFileQuery.DefaultQuery. Files are sorted based on the specified 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

Parameters

query
CommonFileQuery

One of the enumeration values that specifies how to sort the files and determines whether the query is shallow or deep.

Returns

A query result object. Call the GetFilesAsync method of the query result to get the flat list of files, sorted as specified by query. This method returns a list of type IReadOnlyList<StorageFile>. Each file is represented by an item of type StorageFile.

Implements

Attributes

Exceptions

You specified a value other than DefaultQuery from the <xref href="Windows.Storage.Search.CommonFileQuery?text=CommonFileQuery" /> enumeration for a folder that's not a library folder. Check the value of query.

Remarks

A CommonFileQuery sorts files based on specific file attributes (like title or date) quickly and easily.

When you specify the DefaultQuery option from the CommonFileQuery enumeration, this query is a shallow query that returns only files in the current folder. When you specify another value from the CommonFileQuery enumeration, this query is a deep query that returns a flattened list of files from the current folder and from its subfolders.

Tip

Some of the values from the CommonFileQuery enumeration can only be used with a library folder (such as the Pictures library) or the Homegroup folder. In addition to the DefaultQuery option, you can use only the OrderByName and OrderBySearchRank options with a folder that's not a library folder.

For a list of methods that identifies shallow queries and deep queries, see the Remarks in the topic GetFilesAsync.

To check whether a specific CommonFileQuery is available, call the folder's IsCommonFileQuerySupported method.

You can also get a list of files in the current folder asynchronously by calling one of the GetFilesAsync methods.

For Windows Server 2012 You must install indexer components to use some CommonFileQuery values because indexer components are not installed by default.

See also

Applies to