0

I am using FolderBrowserDialog from Windows Forms to open up a dialog so that user can choose a custom folder. Unfortunately, the FolderBrowserDialog has some arhaic design which gives users no option to paste a path. Currently it looks similar to this:

enter image description here

I am after something with an option to paste path:

enter image description here

This is my current code:

using (var dialog = new System.Windows.Forms.FolderBrowserDialog()) {
   string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
   dialog.SelectedPath = Global.DestinationFolder;
   System.Windows.Forms.DialogResult result = dialog.ShowDialog();
}
thatguy
  • 21,059
  • 6
  • 30
  • 40
Robert Segdewick
  • 543
  • 5
  • 17

1 Answers1

2

You cannot customize the FolderBrowserDialog from Windows Forms like that. Once there was the WindowsAPICodePack that included the CommonOpenFileDialog, which had an option for folder selection that would display exactly the dialog you want, but it is not available anymore. You can still use it via the unofficial WindowsAPICodePack-Shell NuGet package.

var folderBrowser = new CommonOpenFileDialog { IsFolderPicker = true }

Another option is to move to .NET Core >= 3.0, where the FolderBrowserDialog in Windows Forms was modernized. You need to add Windows Forms to your WPF project to be able to access it.

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWPF>true</UseWPF>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
</Project>

Finally, if there are no other alternatives, you can use native code to create an IFileOpenDialog and set its option to pick folders only. You can find general information about it here.

thatguy
  • 21,059
  • 6
  • 30
  • 40