WinUI 3 (WindowsAppSDK) FileOpenPicker doesn't work for app that runs under Administrator privileges.
Trying to use a FileOpenPicker while running the app as Administrator will crash the app
In that thread mr. @castorix said:
I found old links... but they cutted the code :-( I will try to re-post, but you can find various implementations by typing for example in Google "IFileOpenDialog comimport github"
I found several good implementations:
This project is IFileOpenDialog interpretation of HedgeModManager solution. I made File pick dialog and folder pick dialog.
Add project to your solution and set dependencies.
using Pickers;
...
// Set filters.
var filters = new List<string> { "*.mp3", "*.wav" };
// Window handle
var _handle = Process.GetCurrentProcess().MainWindowHandle;
// Create dialog.
var openPicker = new FileOpenPicker(_handle);
// Show dialog.
var file = openPicker.Show(filters);
// Result example: C:\Users\SomeUser\Documents\HelloWorld.mp3 or string.Empty.
using Pickers;
...
// Set filters.
var filters = new List<string> { "*.mp3", "*.wav" };
// Set default file name
var defaultName = "song.mp3"
// Window handle
var _handle = Process.GetCurrentProcess().MainWindowHandle;
// Create dialog.
var openPicker = new FileSavePicker(_handle);
// Show dialog.
var file = openPicker.Show(filters, defaultName);
// Result example: C:\Users\SomeUser\Documents\HelloWorld.mp3 or string.Empty.
using Pickers;
...
// Window handle
var _handle = Process.GetCurrentProcess().MainWindowHandle;
// Create dialog.
var openPicker = new FolderPicker(_handle);
// Show dialog.
var path = openPicker.Show();
// Result example: C:\Users\SomeUser\Documents or string.Empty.
- .NET 7 or higher.
- Windows only.
Tested in WinUI 3 app (WindowsAppSDK 1.4.240211001) on Windows 11 23H2.