Windows API Code Pack-Shell: using Microsoft.WindowsAPICodePack.Dialogs; ... var dialog = new CommonOpenFileDialog(); dialog.IsFolderPicker = true; CommonFileDialogResult result = dialog.ShowDialog();
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(); } } } }
// 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; }