我正在尝试使用 CommonOpenFileDialog
来允许用户选择一个文件夹,但也可以在对话框中查看该文件夹内的文件。目前我的代码只允许用户选择一个文件夹,但其中的文件是隐藏的,这导致用户认为他们选择了错误的(空)文件夹。
using (var dlg = new CommonOpenFileDialog()
{
Title = "Select folder to import from",
AllowNonFileSystemItems = true,
IsFolderPicker = true,
EnsurePathExists = true,
Multiselect = false,
ShowPlacesList = true
})
{
if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
{
//Do things with the selected folder and the files within it...
}
}
我如何才能实现针对 Windows 10 的应用程序的目标?
注意: 有一个非常相似的问题,标题为“How can I make CommonOpenFileDialog select folders only, but still show files?”。这个问题已经有了很好的答案,但是,没有一个解决方案适用于 Windows 10。由于现有问题已过时(9 多年前提出)并且不适用于 Windows 10,因此此问题专门要求提供适用于 Windows 10 设备的解决方案。
最佳答案
我认为 CommonOpenFileDialog 不可能做到这一点
请参阅:How to use IFileDialog with FOS_PICKFOLDER while still displaying file names in the dialog
唯一的方法是创建您自己的自定义对话框,或者您可以使用带 P/Invoke 的文件夹对话框(基于 SHBrowseForFolder,如 FolderBrowserDialog
)。
请参阅:http://www.pinvoke.net/default.aspx/shell32.shbrowseforfolder
从上面的链接复制类并添加选项 BIF_BROWSEINCLUDEFILES
。
public string SelectFolder(string caption, string initialPath, IntPtr parentHandle)
{
_initialPath = initialPath;
...
bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_SHAREABLE | BIF_BROWSEINCLUDEFILES;
不幸的是,现在文件只显示在对话框中,如 FolderBrowserDialog
。
关于c# - 在 Windows 10 中使用 CommonOpenFileDialog 选择文件夹但仍显示文件夹内的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65727346/