|
| 1 | +FileOpenPicker Class |
| 2 | +=== |
| 3 | + |
| 4 | +# Background |
| 5 | + |
| 6 | +Namespace: [Microsoft.Windows.Storage.Pickers](./Microsoft.Windows.Storage.Pickers.md) |
| 7 | + |
| 8 | +Represents a UI element that lets the user choose and open files. |
| 9 | + |
| 10 | +Supports specifying the initial location, extension filters, and text on commit button. |
| 11 | + |
| 12 | +# API Pages |
| 13 | + |
| 14 | +## Definition |
| 15 | + |
| 16 | +```C# |
| 17 | +runtimeclass FileOpenPicker |
| 18 | +{ |
| 19 | + FileOpenPicker(Microsoft.UI.WindowId windowId); |
| 20 | + |
| 21 | + string CommitButtonText; |
| 22 | + string SettingsIdentifier; |
| 23 | + IVector<string> FileTypeFilter{ get; }; |
| 24 | + PickerLocationId SuggestedStartLocation; |
| 25 | + PickerViewMode ViewMode; |
| 26 | + |
| 27 | + Windows.Foundation.IAsyncOperation<PickFileResult> PickSingleFileAsync(); |
| 28 | + Windows.Foundation.IAsyncOperation<IVectorView<PickFileResult>> PickMultipleFilesAsync(); |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +## Constructor |
| 33 | + |
| 34 | +### Examples |
| 35 | +C# |
| 36 | + |
| 37 | +```C# |
| 38 | +using Microsoft.Windows.Storage.Pickers; |
| 39 | + |
| 40 | +var openPicker = new FileOpenPicker(this.AppWindow.Id) |
| 41 | +{ |
| 42 | + // (Optional) specify the initial location. |
| 43 | + // If not specified, using PickerLocationId.Unspecified by default. |
| 44 | + SuggestedStartLocation = PickerLocationId.DocumentsLibrary, |
| 45 | + |
| 46 | + // (Optional) specify the text displayed on commit button. If not specified, use system default. |
| 47 | + CommitButtonText = "Choose selected files", |
| 48 | + |
| 49 | + // (Optional) specify file extensions filters. If not specified, default to all (*.*) |
| 50 | + FileTypeFilter = { ".txt", ".pdf", ".doc", ".docx" }, |
| 51 | +}; |
| 52 | +``` |
| 53 | + |
| 54 | +C++ |
| 55 | + |
| 56 | +```C++ |
| 57 | +#include <winrt/Microsoft.Windows.Storage.Pickers.h> |
| 58 | +using namespace winrt::Microsoft::Windows::Storage::Pickers; |
| 59 | + |
| 60 | +FileOpenPicker openPicker(AppWindow().Id()); |
| 61 | + |
| 62 | +// (Optional) specify the initial location. |
| 63 | +// If not specified, using PickerLocationId.Unspecified by default. |
| 64 | +openPicker.SuggestedStartLocation(PickerLocationId::DocumentsLibrary); |
| 65 | + |
| 66 | +// (Optional) specify the text displayed on commit button. If not specified, use system default. |
| 67 | +openPicker.CommitButtonText(L"Choose selected files"); |
| 68 | + |
| 69 | +// (Optional) specify file extensions filters. If not specified, default to all (*.*) |
| 70 | +openPicker.FileTypeFilter().Append(L".txt"); |
| 71 | +openPicker.FileTypeFilter().Append(L".pdf"); |
| 72 | +openPicker.FileTypeFilter().Append(L".doc"); |
| 73 | +openPicker.FileTypeFilter().Append(L".docx"); |
| 74 | +``` |
| 75 | +
|
| 76 | +## FileOpenPicker.PickSingleFileAsync |
| 77 | +
|
| 78 | +Displays a UI element that allows user to choose and open one file. |
| 79 | +
|
| 80 | +Returns a light weight object that has the path of the picked file. |
| 81 | +
|
| 82 | +Return null if the file dialog was cancelled or closed without selection. |
| 83 | +
|
| 84 | +### Examples |
| 85 | +
|
| 86 | +C# |
| 87 | +
|
| 88 | +```C# |
| 89 | +using Microsoft.Windows.Storage.Pickers; |
| 90 | +
|
| 91 | +var openPicker = new FileOpenPicker(this.AppWindow.Id); |
| 92 | +
|
| 93 | +var result = await openPicker.PickSingleFileAsync(); |
| 94 | +if (result is not null) |
| 95 | +{ |
| 96 | + var content = System.IO.File.ReadAllText(result.Path); |
| 97 | +} |
| 98 | +else |
| 99 | +{ |
| 100 | + // error handling. |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +C++ |
| 105 | +```C++ |
| 106 | +#include <winrt/Microsoft.Windows.Storage.Pickers.h> |
| 107 | +#include <fstream> |
| 108 | +#include <string> |
| 109 | +using namespace winrt::Microsoft::Windows::Storage::Pickers; |
| 110 | + |
| 111 | +FileOpenPicker openPicker(AppWindow().Id()); |
| 112 | +auto& result{ co_await openPicker.PickSingleFileAsync() }; |
| 113 | +if (result) |
| 114 | +{ |
| 115 | + std::ifstream fileReader(result.Path().c_str()); |
| 116 | + std::string text((std::istreambuf_iterator<char>(fileReader)), std::istreambuf_iterator<char>()); |
| 117 | + winrt::hstring hText = winrt::to_hstring(text); |
| 118 | +} |
| 119 | +else |
| 120 | +{ |
| 121 | + // error handling. |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## FileOpenPicker.PickMultipleFilesAsync |
| 126 | + |
| 127 | +Displays a UI element that allows user to choose and open multiple files. |
| 128 | + |
| 129 | +Returns a collection of light weight objects that has the path of picked files. |
| 130 | + |
| 131 | +Return an empty list (Count = 0) if the file dialog's cancelled or closed without selection. |
| 132 | + |
| 133 | +### Examples |
| 134 | + |
| 135 | +C# |
| 136 | + |
| 137 | +```C# |
| 138 | +using Microsoft.Windows.Storage.Pickers; |
| 139 | + |
| 140 | +var openPicker = new FileOpenPicker(this.AppWindow.Id); |
| 141 | + |
| 142 | +var results = await openPicker.PickMultipleFilesAsync(); |
| 143 | +if (results.Count > 0) |
| 144 | +{ |
| 145 | + var pickedFilePaths = results.Select(f => f.Path); |
| 146 | + foreach (var path in pickedFilePaths) |
| 147 | + { |
| 148 | + var content = System.IO.File.ReadAllText(path); |
| 149 | + } |
| 150 | +} |
| 151 | +else |
| 152 | +{ |
| 153 | + // error handling. |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +C++ |
| 158 | +```C++ |
| 159 | +#include <winrt/Microsoft.Windows.Storage.Pickers.h> |
| 160 | +#include <fstream> |
| 161 | +#include <string> |
| 162 | +using namespace winrt::Microsoft::Windows::Storage::Pickers; |
| 163 | + |
| 164 | +FileOpenPicker openPicker(AppWindow().Id()); |
| 165 | +auto& results{ co_await openPicker.PickMultipleFilesAsync() }; |
| 166 | +if (results.Size() > 0) |
| 167 | +{ |
| 168 | + for (auto const& result : results) |
| 169 | + { |
| 170 | + std::ifstream fileReader(result.Path().c_str()); |
| 171 | + std::string text((std::istreambuf_iterator<char>(fileReader)), std::istreambuf_iterator<char>()); |
| 172 | + winrt::hstring hText = winrt::to_hstring(text); |
| 173 | + } |
| 174 | +} |
| 175 | +else |
| 176 | +{ |
| 177 | + // error handling. |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +# See Also |
| 182 | + |
| 183 | +[PickFileResult](./PickFileResult.md) |
0 commit comments