FileOpenPicker.PickSingleFileAsync Metodo

Definizione

Visualizza una finestra di dialogo di selezione apri file che consente agli utenti di selezionare e aprire un singolo file.

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

Valori restituiti

Restituisce un oggetto PickFileResult contenente il percorso del file selezionato.

Restituisce null se la finestra di dialogo del file è stata annullata o chiusa senza selcting un file.

Esempio

Nell'esempio seguente viene illustrato come usare il metodo PickSingleFileAsync per aprire una finestra di dialogo selezione file e leggere il contenuto del file selezionato:

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
}

Si applica a