[英]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.