简体   繁体   English

如何使CommonOpenFileDialog仅选择文件夹,但仍显示文件?

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

I am using Microsoft's CommonOpenFileDialog to allow users to select a Folder, but no files are visible when the dialog comes up. 我正在使用Microsoft的CommonOpenFileDialog允许用户选择文件夹,但是出现对话框时没有文件可见。 Is it possible to show files as well as folders when IsFolderPicker is set to true? IsFolderPicker设置为true时,是否可以显示文件和文件夹?

My current code looks like this 我当前的代码如下所示

var dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;

if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
    SelectedFolderPath = dialog.FileName;
}

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! 编辑:神圣2年前蝙蝠侠!


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

Not very sure if it even possible to do in a standard way, but even considering that yes, think about UI . 不是很确定是否可以以标准方式进行操作,但即使是这样,也请考虑一下UI Seeing contemporary folders and files in one place, but be able to select only folders, doesn't seem to me a good UI. 在一个地方只能看到当代的文件夹和文件,但是只能选择文件夹,在我看来,这并不是一个很好的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. 希望这可以帮助。

如果只希望用户选择一个文件夹,是否考虑过使用FolderBrowserDialog?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 CommonOpenFileDialog 到 select 一个文件夹,但仍显示 Windows 10 文件夹中的文件 - Using a CommonOpenFileDialog to select a folder but still show files within the folder in Windows 10 如何让CommonOpenFileDialog的InitialDirectory成为用户的MyDocuments路径,而不是Libraries \\ Documents? - How can I get the CommonOpenFileDialog's InitialDirectory to be the user's MyDocuments path, instead of Libraries\Documents? 如何删除包含文件的文件夹? - How can I delete folders with files in it? 如何将文件夹文件加载到ListView中? - How can I load a folders files into a ListView? 如何强制用户只能访问 select 个多媒体文件? - How can I force user only be able to select multimedia files? 如何使用C#递归计算Windows中的文件和文件夹? - How can I recursively count files and folders in Windows with C#? 如何检查仅具有列表访问权限的文件夹? - How can I check for folders that have ONLY List access? 如何从所有文件夹中获取所有文件? - How can I get all files from all folders? 如何在卸载时通过Wix删除生成的文件夹和文件? - How can I delete generated folders and files via Wix on uninstall? 如何使datagridview一次只能选择同一列中的单元格? - How can I make datagridview can only select the cells in the same column at a time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM