File Dialogs
The file dialog functionality is accessed through the StorageProvider service API, which is available from the Window or TopLevel classes. This page shows only basic usage and for more information about this API please visit StorageProvider page.
OpenFilePickerAsync
This method opens a file picker dialog, allowing the user to select a file. FilePickerOpenOptions defines options that are passed to the OS dialog.
public class MyView : UserControl
{
private async void OpenFileButton_Clicked(object sender, RoutedEventArgs args)
{
// Get top level from the current control. Alternatively, you can use Window reference instead.
var topLevel = TopLevel.GetTopLevel(this);
// Start async operation to open the dialog.
var files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
Title = "Open Text File",
AllowMultiple = false
});
if (files.Count >= 1)
{
// Open reading stream from the first file.
await using var stream = await files[0].OpenReadAsync();
using var streamReader = new StreamReader(stream);
// Reads all the content of file as a text.
var fileContent = await streamReader.ReadToEndAsync();
}
}
}
SaveFilePickerAsync
This method opens a file save dialog, allowing the user to save a file. FilePickerSaveOptions defines options that are passed to the OS dialog.
Example
public class MyView : UserControl
{
private async void SaveFileButton_Clicked(object sender, RoutedEventArgs args)
{
// Get top level from the current control. Alternatively, you can use Window reference instead.
var topLevel = TopLevel.GetTopLevel(this);
// Start async operation to open the dialog.
var file = await topLevel.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Title = "Save Text File"
});
if (file is not null)
{
// Open writing stream from the file.
await using var stream = await file.OpenWriteAsync();
using var streamWriter = new StreamWriter(stream);
// Write some content to the file.
await streamWriter.WriteLineAsync("Hello World!");
}
}
}
SaveFilePickerWithResultAsync
This method works like SaveFilePickerAsync but also returns which file type filter the user selected. This is useful when the file extension depends on the user's choice (for example, exporting as PNG vs JPEG).
Example
public class MyView : UserControl
{
private async void ExportButton_Clicked(object sender, RoutedEventArgs args)
{
var topLevel = TopLevel.GetTopLevel(this);
var result = await topLevel.StorageProvider.SaveFilePickerWithResultAsync(new FilePickerSaveOptions
{
Title = "Export Image",
FileTypeChoices = new[]
{
new FilePickerFileType("PNG Image") { Patterns = new[] { "*.png" } },
new FilePickerFileType("JPEG Image") { Patterns = new[] { "*.jpg", "*.jpeg" } },
}
});
if (result.StorageFile is not null)
{
// result.SelectedFileType contains the filter the user chose
var format = result.SelectedFileType?.Name; // e.g., "PNG Image"
await using var stream = await result.StorageFile.OpenWriteAsync();
// Save in the chosen format...
}
}
}
The returned SaveFilePickerResult struct contains:
| Property | Type | Description |
|---|---|---|
StorageFile | IStorageFile? | The saved file, or null if the user cancelled. |
SelectedFileType | FilePickerFileType? | The file type filter the user selected in the dialog. |
For more information on StorageProvider service including on how to keep access to the picked files and what possible options are supported, please visit StorageProvider documentation page and subpages.
The provided examples directly access the StorageProvider API inside the ViewModel for learning purposes. In a real-world application, it's recommended to adhere to MVVM principles by creating service classes and locating them with Dependency Injection / Inversion of Control (DI/IoC). Please refer to the IoCFileOps and DepInject projects for samples of how to achieve this.
See also
StorageProvider: Full storage provider API reference.- File Picker Options: Configuring file type filters and dialog options.
- Bookmarks: Persisting access to picked files and folders.