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

旧街凉风 提交于 2019-12-17 19:33:39

问题


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;
}

回答1:


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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/8142109/how-can-i-make-commonopenfiledialog-select-folders-only-but-still-show-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!