-1

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?

Curtis
  • 5,794
  • 8
  • 50
  • 77

1 Answers1

1

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;
}
emoacht
  • 2,764
  • 1
  • 13
  • 24