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.
I created a UserControl which is used like this:
<UtilitiesWPF:FolderEntry Text="{Binding Path=LogFolder}" Description="Folder for log files"/>
The xaml source looks like this:
<UserControl x:Class="Utilities.WPF.FolderEntry" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <DockPanel> <Button Margin="0" Padding="0" DockPanel.Dock="Right" Width="Auto" Click="BrowseFolder">...</Button> <TextBox Height="Auto" HorizontalAlignment="Stretch" DockPanel.Dock="Right" Text="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" /> </DockPanel></UserControl>
and the code-behind
public partial class FolderEntry { public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(FolderEntry), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); public static DependencyProperty DescriptionProperty = DependencyProperty.Register("Description", typeof(string), typeof(FolderEntry), new PropertyMetadata(null)); public string Text { get { return GetValue(TextProperty) as string; } set { SetValue(TextProperty, value); }} public string Description { get { return GetValue(DescriptionProperty) as string; } set { SetValue(DescriptionProperty, value); } } public FolderEntry() { InitializeComponent(); } private void BrowseFolder(object sender, RoutedEventArgs e) { using (FolderBrowserDialog dlg = new FolderBrowserDialog()) { dlg.Description = Description; dlg.SelectedPath = Text; dlg.ShowNewFolderButton = true; DialogResult result = dlg.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { Text = dlg.SelectedPath; BindingExpression be = GetBindingExpression(TextProperty); if (be != null) be.UpdateSource(); } } } }
Ookii folder dialog can be found at Nuget.
PM> Install-Package Ookii.Dialogs.Wpf
And, example code is as below.
var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog();if (dialog.ShowDialog(this).GetValueOrDefault()){ textBoxFolderPath.Text = dialog.SelectedPath;}
More information on how to use it: https://github.com/augustoproiete/ookii-dialogs-wpf