C# 使用 OpenFileDialog 作为目录,而不是 FolderBrowserDialog
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/600346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using OpenFileDialog for directory, not FolderBrowserDialog
提问by decasteljau
I want to have a Folder browser in my application, but I don'twant to use the FolderBrowserDialog. (For several reasons, such as it's painful to use)
我想在我的应用程序中有一个文件夹浏览器,但我不想使用 FolderBrowserDialog。(有几个原因,比如使用起来很痛苦)
I want to use the standard OpenFileDialog, but modified for the directories.
我想使用标准的 OpenFileDialog,但针对目录进行了修改。
As an example, μTorrenthas a nice implementation of it (Preferences/Directories/Put new downloads in:). The standard Open File Dialog enable the user to:
例如,μTorrent有一个很好的实现(Preferences/Directories/Put new downloads in:)。标准的打开文件对话框使用户能够:
- paste full paths in the text field at bottom
- use "Favorite Links" bar on Vista
- use Search on Vista
- auto remember last directory
- more...
- 在底部的文本字段中粘贴完整路径
- 在 Vista 上使用“收藏夹链接”栏
- 在 Vista 上使用搜索
- 自动记住上一个目录
- 更多的...
Does anybody know how to implement this? In C#.
有谁知道如何实现这一点?在 C# 中。
回答by Josip Medved
I am not sure about uTorrent but this sounds pretty much like new Vista's IFileDialogwith FOS_PICKFOLDERS option set. Generic C# code for it would go something like:
我不确定 uTorrent,但这听起来很像带有 FOS_PICKFOLDERS 选项集的新 Vista 的IFileDialog。它的通用 C# 代码类似于:
var frm = (IFileDialog)(new FileOpenDialogRCW());
uint options;
frm.GetOptions(out options);
options |= FOS_PICKFOLDERS;
frm.SetOptions(options);
if (frm.Show(owner.Handle) == S_OK) {
IShellItem shellItem;
frm.GetResult(out shellItem);
IntPtr pszString;
shellItem.GetDisplayName(SIGDN_FILESYSPATH, out pszString);
this.Folder = Marshal.PtrToStringAuto(pszString);
}
Full code can be found here.
完整代码可以在这里找到。
回答by Alex Essilfie
See this answerby leetNightShadefor a working solution.
请参阅leetNightShade 的这个答案以获得有效的解决方案。
There are three things I believe make this solution much better than all the others.
我相信有三件事使这个解决方案比所有其他解决方案都要好得多。
- It is simple to use.It only requires you include two files (which can be combined to one anyway) in your project.
- It falls back to the standard FolderBrowserDialogwhen used on XP or older systems.
- The author grants permission to use the code for any purpose you deem fit.
There's no license as such as you are free to take and do with the code what you will.
- 使用起来很简单。它只需要您在项目中包含两个文件(无论如何都可以合并为一个)。
- 在 XP 或更旧的系统上使用时,它会回退到标准的FolderBrowserDialog。
- 作者授权将代码用于您认为合适的任何目的。
没有任何许可证,您可以随意使用代码并随心所欲。
Download the code here.
在此处下载代码。
回答by Koray
var dlg = new Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog();
dlg.IsFolderPicker = true;