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

例外

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

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

路径不能采用 uri 格式, (例如 /Assets) 。 检查 name 的值。

示例

以下示例演示如何通过调用 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));
});

适用于

另请参阅