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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With