FileOpenPicker.PickSingleFileAsync メソッド

定義

ユーザーが 1 つのファイルを選択して開くことができるファイルを開くピッカー ダイアログを表示します。

public:
 virtual IAsyncOperation<PickFileResult ^> ^ PickSingleFileAsync() = PickSingleFileAsync;
IAsyncOperation<PickFileResult> PickSingleFileAsync();
public IAsyncOperation<PickFileResult> PickSingleFileAsync();
function pickSingleFileAsync()
Public Function PickSingleFileAsync () As IAsyncOperation(Of PickFileResult)

返品

選択したファイルのパスを含む PickFileResult オブジェクトを返します。

ファイル ダイアログが取り消されたか、ファイルを削除せずに閉じられた場合は null を返します。

次の例では、 PickSingleFileAsync メソッドを 使用してファイル ピッカー ダイアログを開き、選択したファイルの内容を読み取る方法を示します。

using Microsoft.Windows.Storage.Pickers;

...

var openPicker = new FileOpenPicker(this.AppWindow.Id);

var result = await openPicker.PickSingleFileAsync();
if (result is not null)
{
    var content = System.IO.File.ReadAllText(result.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& result{ co_await openPicker.PickSingleFileAsync() };
if (result)
{
    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
}

適用対象