wpf - Open directory dialog

ID : 10345

viewed : 171

Tags : wpffilesystemsdialogwpf





Top 5 Answer for wpf - Open directory dialog

vote vote

93

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.

vote vote

90

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();             }         }     }  } 
vote vote

79

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

vote vote

69

Ookii Dialogs includes a dialog for selecting a folder (instead of a file):

Ookii Dialogs Select Folder Screenshot

https://github.com/ookii-dialogs

vote vote

57

For those who don't want to create a custom dialog but still prefer a 100% WPF way and don't want to use separate DDLs, additional dependencies or outdated APIs, I came up with a very simple hack using the Save As dialog.

No using directive needed, you may simply copy-paste the code below !

It should still be very user-friendly and most people will never notice.

The idea comes from the fact that we can change the title of that dialog, hide files, and work around the resulting filename quite easily.

It is a big hack for sure, but maybe it will do the job just fine for your usage...

In this example I have a textbox object to contain the resulting path, but you may remove the related lines and use a return value if you wish...

// Create a "Save As" dialog for selecting a directory (HACK) var dialog = new Microsoft.Win32.SaveFileDialog(); dialog.InitialDirectory = textbox.Text; // Use current value for initial dir dialog.Title = "Select a Directory"; // instead of default "Save As" dialog.Filter = "Directory|*.this.directory"; // Prevents displaying files dialog.FileName = "select"; // Filename will then be "select.this.directory" if (dialog.ShowDialog() == true) {     string path = dialog.FileName;     // Remove fake filename from resulting path     path = path.Replace("\\select.this.directory", "");     path = path.Replace(".this.directory", "");     // If user has changed the filename, create the new directory     if (!System.IO.Directory.Exists(path)) {         System.IO.Directory.CreateDirectory(path);     }     // Our final value is in path     textbox.Text = path; } 

The only issues with this hack are :

  • Acknowledge button still says "Save" instead of something like "Select directory", but in a case like mines I "Save" the directory selection so it still works...
  • Input field still says "File name" instead of "Directory name", but we can say that a directory is a type of file...
  • There is still a "Save as type" dropdown, but its value says "Directory (*.this.directory)", and the user cannot change it for something else, works for me...

Most people won't notice these, although I would definitely prefer using an official WPF way if microsoft would get their heads out of their asses, but until they do, that's my temporary fix.

Top 3 video Explaining wpf - Open directory dialog







Related QUESTION?