StorageFolder.GetFileAsync(String) StorageFolder.GetFileAsync(String) StorageFolder.GetFileAsync(String) StorageFolder.GetFileAsync(String) StorageFolder.GetFileAsync(String) Method

Definition

Gets the file with the specified name from the current folder.

public : IAsyncOperation<StorageFile> GetFileAsync(Platform::String name)
IAsyncOperation<StorageFile> GetFileAsync(winrt::hstring name) const;
public IAsyncOperation<StorageFile> GetFileAsync(String name)
Public Function GetFileAsync(name As String) As IAsyncOperation(Of StorageFile)
Windows.Storage.StorageFolder.getFileAsync(name).done( /* Your success and error handlers */ );
Parameters
name
String String String

The name (or path relative to the current folder) of the file to get.

Returns

When this method completes successfully, it returns a StorageFile that represents the specified file.

Exceptions
FileNotFoundException FileNotFoundException FileNotFoundException

The specified file does not exist. Check the value of name.

UnauthorizedAccessException UnauthorizedAccessException UnauthorizedAccessException

You don't have permission to access the specified file.

ArgumentException ArgumentException ArgumentException

The path cannot be in Uri format (for example, /image.jpg). Check the value of name.

Examples

The following example shows how to get a file from the current folder by calling the GetFileAsync method. This example also shows how to get a file from a subfolder of the current folder by providing a relative path.

using Windows.Storage;
using System.Threading.Tasks;

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

// Get the app's manifest file from the current folder.
string name = "AppxManifest.xml";
StorageFile manifestFile = await appFolder.GetFileAsync(name);

// Get a file from a subfolder of the current folder
// by providing a relative path.
string image = @"Assets\Logo.scale-100.png";
StorageFile logoImage = await appFolder.GetFileAsync(image);
IAsyncAction MainPage::ExampleCoroutineAsync()
{
    // Get the app's installation folder.
    Windows::Storage::StorageFolder appFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };

    // Get the app's manifest file from the current folder.
    std::wstring name{ L"AppxManifest.xml" };
    Windows::Storage::StorageFile manifest{ co_await appFolder.GetFileAsync(name) };
    // Do something with the manifest file.
}
// Get the app's installation folder
StorageFolder^ appFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;

// Get the app's manifest file from the current folder
String^ name = "AppxManifest.xml";
create_task(appFolder->GetFileAsync(name)).then([=](StorageFile^ manifest){
 //Do something with the manifest file  
});
// Get the app's installation folder.
var appFolder = Windows.ApplicationModel.Package.current.installedLocation;

// Get the app's manifest file from the current folder.
var name = "AppxManifest.xml";
var manifestFilePromise = appFolder.getFileAsync(name);
var logoImagePromise = manifestFilePromise.then(function getFileSuccess(manifestFile) {
    // Get a file from a subfolder of the current folder
    // by providing a relative path.
    var image = "images\\Logo.scale-100.png";
    return appFolder.getFileAsync(image);
});
logoImagePromise.done(function (logoImage) {
    console.log(logoImage.name);
});

Remarks

To get an item that's a file or a folder, call the GetItemAsync method.

See Also