StorageFolder.GetFileAsync(String) 方法

定义

从当前文件夹中获取具有指定名称的文件。

public:
 virtual IAsyncOperation<StorageFile ^> ^ GetFileAsync(Platform::String ^ name) = GetFileAsync;
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<StorageFile> GetFileAsync(winrt::hstring const& name);
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<StorageFile> GetFileAsync(string name);
function getFileAsync(name)
Public Function GetFileAsync (name As String) As IAsyncOperation(Of StorageFile)

参数

name
String

Platform::String

winrt::hstring

相对于要获取的文件的当前文件夹) 的名称 (或路径。

返回

此方法成功完成后,将返回表示指定文件的 StorageFile

实现

M:Windows.Storage.IStorageFolder.GetFileAsync(System.String) M:Windows.Storage.IStorageFolder.GetFileAsync(Platform::String) M:Windows.Storage.IStorageFolder.GetFileAsync(winrt::hstring)
属性

例外

指定的文件不存在。 检查 name 的值。

您没有访问指定文件的权限。 有关详细信息,请参阅 文件访问权限

路径不能为 uri 格式, (例如 /image.jpg) 。 检查 name 的值。

示例

以下示例演示如何通过调用 GetFileAsync 方法从当前文件夹中获取文件。 此示例还演示如何通过提供相对路径从当前文件夹的子文件夹中获取文件。

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

注解

若要获取文件或文件夹的项目,请调用 GetItemAsync 方法。

适用于

另请参阅