问题描述
在 VS .NET 中,当您为项目选择文件夹时,会显示一个看起来像 OpenFileDialog 或 SaveFileDialog 的对话框,但设置为仅接受文件夹.自从我看到这个,我就想知道它是如何完成的.我知道 FolderBrowserDialog,但我从来没有真正喜欢过那个对话框.它开始时太小,无法让我利用能够键入路径的优势.
In VS .NET, when you are selecting a folder for a project, a dialog that looks like an OpenFileDialog or SaveFileDialog is displayed, but is set up to accept only folders. Ever since I've seen this I've wanted to know how it's done. I am aware of the FolderBrowserDialog, but I've never really liked that dialog. It starts too small and doesn't let me take advantage of being able to type a path.
我几乎可以肯定,目前还没有办法从 .NET 中执行此操作,但我也很好奇您如何从非托管代码中执行此操作.如果没有从头开始完全重新实现对话框,您如何修改对话框以使其具有这种行为?
I'm almost certain by now there's not a way to do this from .NET, but I'm just as curious how you do it from unmanaged code as well. Short of completely reimplementing the dialog from scratch, how do you modify the dialog to have this behavior?
我还想重申我知道 FolderBrowserDialog 但有时我不喜欢使用它,除了真的很好奇如何以这种方式配置对话框.告诉我只使用 FolderBrowserDialog 有助于我保持一致的 UI 体验,但不能满足我的好奇心,因此它不会算作答案.
I'd also like to restate that I am aware of the FolderBrowserDialog but sometimes I don't like to use it, in addition to being genuinely curious how to configure a dialog in this manner. Telling me to just use the FolderBrowserDialog helps me maintain a consistent UI experience but doesn't satisfy my curiosity so it won't count as an answer.
这也不是特定于 Vista 的;我从 VS .NET 2003 开始就看到了这个对话框,所以它在 Win2k 和 WinXP 中是可行的.这不是我想知道执行此操作的正确方法"的问题,而是自从我第一次想在 VS 2003 中这样做以来,我一直对此感到好奇"的问题.我知道 Vista 的文件对话框有执行此操作的选项,但它一直在 XP 中工作,所以我知道他们做了 一些事情 让它工作.Vista 特定的答案不是答案,因为问题上下文中不存在 Vista.
It's not a Vista-specific thing either; I've been seeing this dialog since VS .NET 2003, so it is doable in Win2k and WinXP. This is less of a "I want to know the proper way to do this" question and more of a "I have been curious about this since I first wanted to do it in VS 2003" question. I understand that Vista's file dialog has an option to do this, but it's been working in XP so I know they did something to get it to work. Vista-specific answers are not answers, because Vista doesn't exist in the question context.
更新:我接受 Scott Wisniewski 的回答,因为它附带了一个工作示例,但我认为 Serge 指出对话框自定义是值得称赞的(这在 .NET 中固然令人讨厌,但它确实 工作)和 Mark Ransom,感谢他们发现 MS 可能为此任务推出了自定义对话框.
Update: I'm accepting Scott Wisniewski's answer because it comes with a working sample, but I think Serge deserves credit for pointing to the dialog customization (which is admittedly nasty from .NET but it does work) and Mark Ransom for figuring out that MS probably rolled a custom dialog for this task.
推荐答案
我写了一个对话框,叫做 OpenFileOrFolder 对话框,它允许您打开文件夹或文件.
I have a dialog that I wrote called an OpenFileOrFolder dialog that allows you to open either a folder or a file.
如果您将其 AcceptFiles 值设置为 false,则它仅在接受文件夹模式下运行.
If you set its AcceptFiles value to false, then it operates in only accept folder mode.
您可以在此处从 GitHub 下载源代码
这篇关于如何配置 OpenFileDialog 来选择文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!