FileOpenPicker.PickSingleFileAsync 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
顯示檔案開啟選取器對話框,讓使用者選擇並開啟單一檔案。
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
}