Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some way to use a folder seletor (FolderBrowserDialog) in WPF Core?

Tags:

.net-core

wpf

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.

like image 710
Álvaro García Avatar asked Sep 15 '25 07:09

Álvaro García


1 Answers

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
}
like image 169
Max Voisard Avatar answered Sep 18 '25 09:09

Max Voisard