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

3

20

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;
}
Triacid answered 15/11, 2011 at 19:37 Comment(3)
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.Croze
@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.Triacid
@Triacid -- Yes I found this question because that was my exact need: to pick a folder by using its files as context, i.e. so I know I have the right folder.Flout
M
10

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();
Maidstone answered 20/11, 2013 at 17:3 Comment(4)
-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?Hash
Just a note, on Windows 10 not setting DefaultFileName (which is done in the first snippet) makes ShowDialog() throw.Limann
This does not work also in Windows 10, did you test it?Loehr
@Loehr 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.Maidstone
H
0

Not very sure if it even possible to do in a standard way, but even considering that yes, think about UI. Seeing contemporary folders and files in one place, but be able to select only folders, doesn't seem to me a good UI. IMHO it's better, and more "natural" way, to have one control populated with folders, and another (clearly readonly) populated with only files that have to be loaded.

Hope this helps.

Humane answered 15/11, 2011 at 20:45 Comment(2)
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.Triacid
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?Humane
S
-3

If you want the user to select a folder only, have you considered using a FolderBrowserDialog?

Shewchuk answered 15/11, 2011 at 21:2 Comment(2)
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 doesTriacid
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)Triacid

© 2022 - 2024 — McMap. All rights reserved.