你如何配置一个OpenFileDialog选择文件夹?文件夹、OpenFileDialog

2023-09-02 10:17:12 作者:单身狗

在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做到这一点,但我只是好奇你是如何从非托管code做得一样好。短的完全重新实现从无到有的对话框中,你如何修改对话框中有此行为?

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帮助我保持一致的用户界面体验,但不能满足我的好奇心,这样就不会算作一个答案。

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.

更新:我接受斯科特的Wisniewski的答案,因为它带有一个工作示例,但我认为塞尔的功劳指向对话框自定义(这是.NET固然讨厌,但它的确实的工作)和马克赎金为搞清楚那个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.

您也可以下载源代码在这里:

You can download the source for it here:

https://github.com/scottwis/OpenFileOrFolderDialog

如果您遇到任何问题,想要了解它是如何工作的详细信息,或者想贡献变化,给我发电子邮件至:

If you run into any issues, want more information about how it works, or want to contribute changes, send me an email message to:

scott@transactor.com

scott@transactor.com