4

I would like to have a dialog to select a folder in a WPF Core application, but I am not able to find the way.

In a WPF net framework application, I could use FolderBrowserDialog of System.Windows.Forms.

I have read this thread: OpenFileDialog on .NET Core

But for me it is not clear how to use the solution of the mm8 user.

Thanks.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193

1 Answers1

10

Microsoft doesn't provide a folder selector in FolderBrowserDialog by default, which I found surprising. You can download the Windows API Code Pack by going to your Nuget Package Manager and typing in the following commands:

Install-Package WindowsAPICodePack-Core
Install-Package WindowsAPICodePack-ExtendedLinguisticServices
Install-Package WindowsAPICodePack-Sensors
Install-Package WindowsAPICodePack-Shell
Install-Package WindowsAPICodePack-ShellExtensions

Then add references to Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll. Sample code:

using Microsoft.WindowsAPICodePack.Dialogs;

var dlg = new CommonOpenFileDialog();
dlg.Title = "My Title";
dlg.IsFolderPicker = true;
dlg.InitialDirectory = currentDirectory;

dlg.AddToMostRecentlyUsedList = false;
dlg.AllowNonFileSystemItems = false;
dlg.DefaultDirectory = currentDirectory;
dlg.EnsureFileExists = true;
dlg.EnsurePathExists = true;
dlg.EnsureReadOnly = false;
dlg.EnsureValidNames = true;
dlg.Multiselect = false;
dlg.ShowPlacesList = true;

if (dlg.ShowDialog() == CommonFileDialogResult.Ok) 
{
   var folder = dlg.FileName;
   // Do something with selected folder string
}
Max Voisard
  • 1,685
  • 1
  • 8
  • 18
  • 2
    In my case i have only implrted WindowsAPICodePack-Core and Microsoft.WindowsAPICodePack.Shell and it works perfectly. Thanks so much. – Álvaro García Oct 26 '19 at 15:12