How can I make CommonOpenFileDialog select folders only, but still show files?

27,707

Off the top of my head, this is how I did it

  var dialog = new CommonOpenFileDialog
  {
    EnsurePathExists = true,
    EnsureFileExists = false,
    AllowNonFileSystemItems = false,
    DefaultFileName = "Select Folder",
    Title = "Select The Folder To Process"
  };


  dialog.SetOpenButtonText("Select Folder");

  if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
  dirToProcess = Directory.Exists(dialog.FileName) ? dialog.FileName : Path.GetDirectoryName(dialog.FileName);

EDIT: Holy 2 years ago Batman!


Seems like few changes were made, snippet below seems to do the job

var openFolder = new CommonOpenFileDialog();
openFolder.AllowNonFileSystemItems = true;
openFolder.Multiselect = true;
openFolder.IsFolderPicker = true;
openFolder.Title = "Select folders with jpg files";

if (openFolder.ShowDialog() != CommonFileDialogResult.Ok)
{
    MessageBox.Show("No Folder selected");
    return;
}

// get all the directories in selected dirctory
var dirs = openFolder.FileNames.ToArray();
Share:
27,707

Related videos on Youtube

Rachel
Author by

Rachel

"The three chief virtues of a programmer are: Laziness, Impatience and Hubris." - Larry Wall Laziness: I'm too lazy to do the same task repeatedly so write scripts to do that task for me. This makes people think I am intelligent. Impatience: I'm too impatient to wait for my code to run so rewrite the code to improve performance. This makes people think I am a good programmer. Hubris: When someone asks if I can do something I just say Yes, then go find out how to do it (Google!). This makes people think I can do anything. Ultimately, it means I can make a career out of being Lazy, Impatient, and Hubristic.

Updated on July 09, 2022

Comments

  • Rachel
    Rachel almost 2 years

    I am using Microsoft's CommonOpenFileDialog to allow users to select a Folder, but no files are visible when the dialog comes up. Is it possible to show files as well as folders when IsFolderPicker is set to true?

    My current code looks like this

    var dialog = new CommonOpenFileDialog();
    dialog.IsFolderPicker = true;
    
    if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
    {
        SelectedFolderPath = dialog.FileName;
    }
    
    • user1703401
      user1703401 over 12 years
      Why do you want to show files if the user can only pick folders? It isn't an option. Consider setting InitialDirectory to a folder that has at least some sub-directories so the list isn't empty.
    • Rachel
      Rachel over 12 years
      @HansPassant The user is picking the folder which contains files to be processed, and showing the files is a way for users to verify that they have the right folder selected.
  • Rachel
    Rachel over 12 years
    I hate the FolderBrowserDialog for its bad UI and lack of user-friendliness. For example, you can't type the path name if you know it, and it doesn't remember your last selected item the way a FileDialog does
  • Rachel
    Rachel over 12 years
    I don't like this idea because there isn't very much UI space available for displaying lists of files, and users expect to see files inside folders in a OpenFileDialog. Showing them a blank list when they select their folder can often cause users to think they are in the wrong location, or that something happened to their files.
  • Tigran
    Tigran over 12 years
    btw,don't think show them files and DON'T let them to pick any file, is kind of frustrating. What about enabling a Drag&Drop of the folder form outside (let's say Windows Explorer) and make a notion about it in some way on UI?
  • Rachel
    Rachel over 12 years
    In the end, this is what I did. I would have rather created my own custom OpenFolderDialog that was more user friendly, however this wasn't a very important project and I didn't have the time to devote to something like this. I did make the FilePath TextBox editable though, so users could copy/paste a folder path in it (perhaps my biggest pet peeve with FolderBrowserDialog)
  • lambinator
    lambinator about 8 years
    -1 the first solution doesn't allow the user to select the folder, only files. The second solution doesn't show files in the folder as the OP asked. Windows 10, maybe this changed?
  • Rudolfs Bundulis
    Rudolfs Bundulis over 6 years
    Just a note, on Windows 10 not setting DefaultFileName (which is done in the first snippet) makes ShowDialog() throw.
  • S.Serpooshan
    S.Serpooshan almost 5 years
    This does not work also in Windows 10, did you test it?
  • Skinner927
    Skinner927 almost 5 years
    @S.Serpooshan I wrote this in 2013, 2 years before Windows 10 was released, so no it was not tested against Windows 10. Feel free to provide an updated solution. I haven't done Windows development in a few years.