共用方式為


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)
屬性

例外狀況

指定的檔案不存在。 檢查 名稱的值。

您沒有存取指定檔案的許可權。 如需詳細資訊,請參閱 檔案存取權限

路徑不能以 URI 格式 (,例如 /image.jpg) 。 檢查 名稱的值。

範例

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

適用於

另請參閱