共用方式為


StorageFolder.GetFolderAsync(String) 方法

定義

從目前資料夾取得具有指定名稱的子資料夾。

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

參數

name
String

Platform::String

winrt::hstring

相對於目前資料夾的名稱 (或路徑,) 要取得的子資料夾。

傳回

當這個方法順利完成時,它會傳回代表指定子資料夾的 StorageFolder

實作

M:Windows.Storage.IStorageFolder.GetFolderAsync(System.String) M:Windows.Storage.IStorageFolder.GetFolderAsync(Platform::String) M:Windows.Storage.IStorageFolder.GetFolderAsync(winrt::hstring)
屬性

例外狀況

指定的資料夾不存在。 檢查 名稱的值。

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

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

範例

下列範例示範如何藉由呼叫 GetFolderAsync 方法,從目前資料夾取得子資料夾。 此範例也會示範如何藉由提供相對路徑,從目前資料夾的子資料夾取得子資料夾。

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

// Get the app's local folder.
StorageFolder localFolder = ApplicationData.Current.LocalFolder;

// Create 2 levels of subfolders in the app's local folder.
string name1 = "Subfolder1";
StorageFolder subFolder1 = await localFolder.CreateFolderAsync(name1);
string name2 = "Subfolder2";
StorageFolder subFolder2 = await subFolder1.CreateFolderAsync(name2);

// Get the subfolder of the app's local folder.
string level1FolderName = "Subfolder1";
StorageFolder level1Folder = await localFolder.GetFolderAsync(level1FolderName);

// Get the subfolder of the subfolder by providing a relative path.
string level2FolderName = @"Subfolder1\Subfolder2";
StorageFolder level2Folder = await localFolder.GetFolderAsync(level2FolderName);
IAsyncAction MainPage::ExampleCoroutineAsync()
{
    // Get the app's local folder.
    Windows::Storage::StorageFolder localFolder{ Windows::Storage::ApplicationData::Current().LocalFolder() };

    // Create two levels of subfolders in the app's local folder.
    std::wstring name1{ L"Subfolder1" };
    std::wstring name2{ L"Subfolder2" };
    std::wstring level2FolderName{ L"Subfolder1\\Subfolder2" };

    Windows::Storage::StorageFolder subFolder1{ co_await localFolder.CreateFolderAsync(name1) };
    Windows::Storage::StorageFolder subFolder2{ co_await subFolder1.CreateFolderAsync(name2) };
    // Get the subfolder of the subfolder by providing a relative path.
    Windows::Storage::StorageFolder level2FolderFromRelativePath{ co_await localFolder.GetFolderAsync(level2FolderName) };
}
// Get the app's local folder
StorageFolder^ localFolder = ApplicationData::Current->LocalFolder;

// Create 2 levels of subfolders in the app's local folder.
String^ name1 = "Subfolder1";
String^ name2 = "Subfolder2";
String^ level2FolderName = "Subfolder1\\Subfolder2";

create_task(localFolder->CreateFolderAsync(name1)).then([=](StorageFolder^ subFolder1) -> task <StorageFolder^> {
 return create_task(subFolder1->CreateFolderAsync(name2));
}).then([=](StorageFolder^ subFolder2) -> task<StorageFolder^> {
 // Get the subfolder of the subfolder
 // by providing a relative path.
 return create_task(localFolder->GetFolderAsync(level2FolderName));
});

適用於

另請參閱