Open directory dialog
You can use the built-in FolderBrowserDialog class for this. Don't mind that it's in the System.Windows.Forms
namespace.
using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
{
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
}
If you want the window to be modal over some WPF window, see the question How to use a FolderBrowserDialog from a WPF application.
EDIT: If you want something a bit more fancy than the plain, ugly Windows Forms FolderBrowserDialog, there are some alternatives that allow you to use the Vista dialog instead:
Third-party libraries, such as Ookii dialogs (.NET 4.5+)
The Windows API Code Pack-Shell:
using Microsoft.WindowsAPICodePack.Dialogs;
...
var dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;
CommonFileDialogResult result = dialog.ShowDialog();Note that this dialog is not available on operating systems older than Windows Vista, so be sure to check
CommonFileDialog.IsPlatformSupported
first.
Select folder dialog WPF
The FolderBrowserDialog
class from System.Windows.Forms
is the recommended way to display a dialog that allows a user to select a folder.
Until recently, the appearance and behaviour of this dialog was not in keeping with the other file system dialogs, which is one of the reasons why people were reluctant to use it.
The good news is that FolderBrowserDialog
was "modernized" in NET Core 3.0, so is now a viable option for those writing either Windows Forms or WPF apps targeting that version or later.
In .NET Core 3.0, Windows Forms users [sic] a newer COM-based control that was introduced in Windows Vista:
To reference System.Windows.Forms
in a NET Core WPF app, it is necessary to edit the project file and add the following line:
<UseWindowsForms>true</UseWindowsForms>
This can be placed directly after the existing <UseWPF>
element.
Then it's just a case of using the dialog:
using System;
using System.Windows.Forms;
...
using var dialog = new FolderBrowserDialog
{
Description = "Time to select a folder",
UseDescriptionForTitle = true,
SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
+ Path.DirectorySeparatorChar,
ShowNewFolderButton = true
};
if (dialog.ShowDialog() == DialogResult.OK)
{
...
}
FolderBrowserDialog
has a RootFolder
property that supposedly "sets the root folder where the browsing starts from" but whatever I set this to it didn't make any difference; SelectedPath
seemed to be the better property to use for this purpose, however the trailing backslash is required.
Also, the ShowNewFolderButton
property seems to be ignored as well, the button is always shown regardless.
How do I use OpenFileDialog to select a folder?
As a note for future users who would like to avoid using FolderBrowserDialog
, Microsoft once released an API called the WindowsAPICodePack that had a helpful dialog called CommonOpenFileDialog
, that could be set into a IsFolderPicker
mode. The API is available from Microsoft as a NuGet package.
This is all I needed to install and use the CommonOpenFileDialog
. (NuGet handled the dependencies)
Install-Package Microsoft.WindowsAPICodePack-Shell
For the include line:
using Microsoft.WindowsAPICodePack.Dialogs;
Usage:
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.InitialDirectory = "C:\\Users";
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
MessageBox.Show("You selected: " + dialog.FileName);
}
Is there some way to use a folder seletor (FolderBrowserDialog) in WPF Core?
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
}
FolderBrowserDialog in wpf c#
You can use the FolderBrowserDialog
; either explicitly place the namespace in front of the class...
System.Windows.Forms.FolderBrowserDialog browse = new System.Windows.Forms.FolderBrowserDialog();
...or create an alias with regard to your namespace.
Imports [ aliasname = ] namespace
WPF FolderBrowserDialog using MVVM(without code behind)
Do the following changes,
In View.xaml
<Button Content="Browse" Command="{Binding OpenFolderCommand}"/>
In ViewModel.cs
public bool CanOpenFolder()
{
return true;
}
private void OpenFolder()
{
FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && OpenFolderCommand.CanExecute(openFolderDialog.SelectedPath))
{
//OpenFolderCommand.Execute(openFolderDialog.SelectedPath);
FoldernameWithPath = openFolderDialog.SelectedPath;
}
}
Folder Browser Dialog Inital Folder
I did not know before now Ookii dialogs, but after a little search and knowing how common Open Folder Dialog works i suggest that you set your lastOpenFolder to the SelectedPath property
folderDialog.SelectedPath = MyProject.ProgramConfigurationFile.Instance.OpenFolderPath;
But this must be done before folderDialog.ShowDialog(this)
showing the dialog.
So it should look something like
VistaFolderBrowserDialog folderDialog = new VistaFolderBrowserDialog();
folderDialog.Description = "Please select the folder";
folderDialog.UseDescriptionForTitle = true;
if(!string.IsNullOrEmpty(MyProject.ProgramConfigurationFile.Instance.OpenFolderPath))
folderDialog.SelectedPath = myProject.ProgramConfigurationFile.Instance.OpenFolderPath;
if ((bool)folderDialog.ShowDialog(this))
{
string newSelectedFolderPath = folderDialog.SelectedPath;
// Use new folderPath
}
Let me know if this solves it.
I hope I was usefull.
Related Topics
Replace Part of a JSON with Other (Using a String Token)
How to Set Web.Config File to Show Full Error Message
How to Make Chrome Headless After I Login Manually
What's the Fastest Way to Bulk Insert a Lot of Data in SQL Server (C# Client)
How to Remove an Xml Element from File
Getting Current Directory in .Net Web Application
Datatable, How to Conditionally Delete Rows
Login to the Page with Httpwebrequest
How to Avoid Multiple Instances of Windows Form in C#
How to Get a Type's Alias Through Reflection
How to Open Outlook New Mail Window C#
How to Solve Disturbance in My Bot in C#
Adding and Removing Users from Active Directory Groups in .Net
How to Write Output from a Unit Test
Starting and Stopping Iis Express Programmatically