Created
May 20, 2023 09:47
-
-
Save emoacht/613d8ebba80a321c6e9069180c37afc2 to your computer and use it in GitHub Desktop.
Use WinRT's FileOpenPicker and FolderPicker from WPF.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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