FileOpenPicker.PickSingleFileAsync Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Affiche une boîte de dialogue de sélecteur d’ouverture de fichier qui permet aux utilisateurs de sélectionner et d’ouvrir un seul fichier.
public:
virtual IAsyncOperation<PickFileResult ^> ^ PickSingleFileAsync() = PickSingleFileAsync;
IAsyncOperation<PickFileResult> PickSingleFileAsync();
public IAsyncOperation<PickFileResult> PickSingleFileAsync();
function pickSingleFileAsync()
Public Function PickSingleFileAsync () As IAsyncOperation(Of PickFileResult)
Retours
Renvoie un objet PickFileResult contenant le chemin d’accès du fichier sélectionné.
Retourne null si la boîte de dialogue de fichier a été annulée ou fermée sans avoir à fermer un fichier.
Exemples
L’exemple suivant montre comment utiliser la méthode PickSingleFileAsync pour ouvrir une boîte de dialogue sélecteur de fichiers et lire le contenu du fichier sélectionné :
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
}