marticliment / Pickers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pickers

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:

  1. HedgeModManager
  2. ShellFileDialogs

This project is IFileOpenDialog interpretation of HedgeModManager solution. I made File pick dialog and folder pick dialog.

How to use

Add project to your solution and set dependencies.

Open file pick dialog

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.

Save file pick dialog

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.

Folder pick dialog

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.

Requirements

  1. .NET 7 or higher.
  2. Windows only.

Tested in WinUI 3 app (WindowsAppSDK 1.4.240211001) on Windows 11 23H2.

About

License:MIT License


Languages

Language:C# 100.0%