FileOpenPicker.PickMultipleFilesAsync 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
显示允许用户选择和打开多个文件的对话框。
public:
virtual IAsyncOperation<IVectorView<PickFileResult ^> ^> ^ PickMultipleFilesAsync() = PickMultipleFilesAsync;
IAsyncOperation<IVectorView<PickFileResult>> PickMultipleFilesAsync();
public IAsyncOperation<IReadOnlyList<PickFileResult>> PickMultipleFilesAsync();
function pickMultipleFilesAsync()
Public Function PickMultipleFilesAsync () As IAsyncOperation(Of IReadOnlyList(Of PickFileResult))
返回
返回具有所选文件路径的 PickFileResult 对象的集合。
如果取消或关闭对话框而不选择,则返回空列表(IReadOnlyList.Count = 0)。
示例
以下示例演示如何使用 PickMultipleFilesAsync 方法打开文件选取器对话框并读取所选文件的内容:
using Microsoft.Windows.Storage.Pickers;
var openPicker = new FileOpenPicker(this.AppWindow.Id);
var results = await openPicker.PickMultipleFilesAsync();
if (results.Count > 0)
{
var pickedFilePaths = results.Select(f => f.Path);
foreach (var path in pickedFilePaths)
{
var content = System.IO.File.ReadAllText(path);
}
}
else
{
// Add error handling logic here
}
#include <winrt/Microsoft.Windows.Storage.Pickers.h>
#include <fstream>
#include <string>
using namespace winrt::Microsoft::Windows::Storage::Pickers;
FileOpenPicker openPicker(AppWindow().Id());
auto& results{ co_await openPicker.PickMultipleFilesAsync() };
if (results.Size() > 0)
{
for (auto const& result : results)
{
std::ifstream fileReader(result.Path().c_str());
std::string text((std::istreambuf_iterator<char>(fileReader)), std::istreambuf_iterator<char>());
winrt::hstring hText = winrt::to_hstring(text);
}
}
else
{
// Add error handling logic here
}