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

发布于 2024-12-15 10:31:14 字数 471 浏览 2 评论 0原文

我正在使用 Microsoft 的 CommonOpenFileDialog 来允许用户选择文件夹,但出现对话框时看不到任何文件。当 IsFolderPicker 设置为 true 时,是否可以显示文件和文件夹?

我当前的代码如下所示

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作死小能手 2024-12-22 10:31:15

在我的脑海里,这就是我所做的

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

编辑:神圣的两年前蝙蝠侠!


似乎做了一些更改,下面的代码片段似乎可以完成这项工作

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

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();
空气里的味道 2024-12-22 10:31:15

不太确定是否可以以标准方式完成,但即使考虑到可以,请考虑UI。在一个地方查看当代文件夹和文件,但能够选择文件夹,在我看来并不是一个好的用户界面。恕我直言,这是更好、更“自然”的方式,让一个控件填充文件夹,而另一个控件(明显只读)仅填充必须加载的文件。

希望这有帮助。

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.

凡尘雨 2024-12-22 10:31:15

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文