问题描述
在 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.
这篇关于你如何配置一个 OpenFileDialog 来选择文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!