Sprache

FileOpenPicker.PickSingleFileAsync Methode

Definition

Zeigt ein Dialogfeld zum Öffnen der Dateiauswahl an, in dem Benutzer eine einzelne Datei auswählen und öffnen können.

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

Gibt zurück

Gibt ein PickFileResult -Objekt zurück, das den Pfad der ausgewählten Datei enthält.

Gibt NULL zurück, wenn das Dateidialogfeld abgebrochen oder geschlossen wurde, ohne eine Datei zu markieren.

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie Sie die PickSingleFileAsync-Methode verwenden, um ein Dateiauswahldialogfeld zu öffnen und den Inhalt der ausgewählten Datei zu lesen:

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
}

Gilt für: