语言

FolderPicker.PickSingleFolderAsync 方法

定义

显示允许用户选择文件夹的对话框。

public:
 virtual IAsyncOperation<PickFolderResult ^> ^ PickSingleFolderAsync() = PickSingleFolderAsync;
IAsyncOperation<PickFolderResult> PickSingleFolderAsync();
public IAsyncOperation<PickFolderResult> PickSingleFolderAsync();
function pickSingleFolderAsync()
Public Function PickSingleFolderAsync () As IAsyncOperation(Of PickFolderResult)

返回

返回一个 PickFolderResult 对象,该对象包含选取的文件夹的路径。

如果取消或关闭文件夹对话框而不选择,则返回 null 。

示例

以下示例演示如何使用 PickSingleFolderAsync 方法打开文件夹选取器对话框并选择文件夹:

using Microsoft.Windows.Storage.Pickers;

var folderPicker = new FolderPicker(this.AppWindow.Id);
var result = await folderPicker.PickSingleFolderAsync();
if (result is not null)
{
    var path = result.Path;
}
else
{
    // Add error handling logic here
}
#include <winrt/Microsoft.Windows.Storage.Pickers.h>
using namespace winrt::Microsoft::Windows::Storage::Pickers;

FolderPicker folderPicker(AppWindow().Id());
auto& result{ co_await folderPicker.PickSingleFolderAsync() };
if (result)
{
    auto path{ result.Path() };
}
else
{
    // Add error handling logic here
}

注解

如有必要,可以通过使用文件夹路径调用Windows.Storage.StorageFolder.GetFolderFromPathAsync对象,Windows.Storage.StorageFolder将对象转换为PickFolderResult对象。

适用于

另请参阅