Skip to content

Instantly share code, notes, and snippets.

@emoacht
Created May 20, 2023 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emoacht/613d8ebba80a321c6e9069180c37afc2 to your computer and use it in GitHub Desktop.
Save emoacht/613d8ebba80a321c6e9069180c37afc2 to your computer and use it in GitHub Desktop.
Use WinRT's FileOpenPicker and FolderPicker from WPF.
using System;
using System.Threading.Tasks;
using System.Windows.Interop;
using Windows.Storage.Pickers;
public static async Task<string?> OpenFileDialog(System.Windows.Window window)
{
var picker = new FileOpenPicker()
{
ViewMode = PickerViewMode.Thumbnail
};
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
IntPtr hwnd = new WindowInteropHelper(window).Handle;
WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd);
var file = await picker.PickSingleFileAsync();
return file?.Path;
}
public static async Task<string?> OpenFolderDialog(System.Windows.Window window)
{
var picker = new FolderPicker();
picker.FileTypeFilter.Add("*");
IntPtr hwnd = new WindowInteropHelper(window).Handle;
WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd);
var folder = await picker.PickSingleFolderAsync();
return folder?.Path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment