What is the proper substitute for FolderBrowserDialog to use in a .net 6 WPF application?

105 Views Asked by At

I am trying to use correct practices and avoid HACKS in a new .net 6.0 WPF application.

I need a FolderBrowserDialog. There is one in System.Windows.Forms, but by default .net 6 doesn't let you use that. There are workarounds that I've found on Stack Overflow, but I'm just wondering is there a standard FolderBrowserDialog equivalent for .net 6?

1

There are 1 best solutions below

0
emoacht On

There is no standard but WinRT's Windows.Storage.Pickers.FolderPicker is newer.

using System;
using System.Threading.Tasks;
using System.Windows.Interop;

public static async Task<string?> OpenFolderDialog(System.Windows.Window window)
{
    var picker = new Windows.Storage.Pickers.FolderPicker();
    picker.FileTypeFilter.Add("*");

    IntPtr hwnd = new WindowInteropHelper(window).Handle;
    WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd);

    var folder = await picker.PickSingleFolderAsync();
    return folder?.Path;
}