10

In Visual Studio 2008 there is a folder browser dialog that looks like this (very similar to file open dialog):

Dialog

Does anyone know how to invoke it from code?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Alex Reitbort
  • 13,504
  • 1
  • 40
  • 61

4 Answers4

21

If you're using C#, this solution is for you. Source code provided here: http://www.lyquidity.com/devblog/?p=136 (.NET Win 7-style folder select dialog). [Update: if site is dead, here's a wayback machine link.]

How does it work? It turns out that the ability to show a Vista-like dialog is present in .NET but the methods are not public. So the ShowDialog() method uses reflection to call CreateVistaDialog and pass in all the parameters. The technique to do this is taken from the Google code project FED.

You don't need to use a whole library like VistaBridge, or a Windows API code pack, to get a nice Folder Dialogue, just two small source files. Gives you a nice folder dialogue like this:

leetNightshade
  • 2,673
  • 2
  • 36
  • 47
  • 3
    This solution works without using VistaBridge and it provides a fallback for XP and older. – Alex Essilfie Jun 09 '13 at 14:47
  • This is a great solution. It simply works and unlike Windows API code pack this has no license restrictions. Thank you! – jetstream96 Dec 29 '16 at 09:01
  • It should be mentioned that the Reflector class is from [Front-End for Dosbox](http://code.google.com/p/fed/) and this is GPL v2. So it *has* license restrictions. – Gerd K Mar 31 '22 at 13:33
  • July 2023: the linked site, lyquidity.com, appears to be gone. – fadden Jul 27 '23 at 22:45
  • 1
    @fadden Wayback machine and download still works: https://web.archive.org/web/20230408013724/http://www.lyquidity.com/devblog/?p=136 – leetNightshade Jul 30 '23 at 21:01
5

At the end I just used the VistaBridge library to open it.

Alex Reitbort
  • 13,504
  • 1
  • 40
  • 61
1

Is this the pinvoke of SHBrowseForFolder, with the BIF_NEWDIALOGSTYLE style? If so there is an example on that page.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
-2

Drag a FolderBrowserDialog component from the Dialogs tab of the Toolbox to the form. Add this code to you button handler.

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            this.label1.Text = folderBrowserDialog1.SelectedPath;
        }
Sorantis
  • 14,496
  • 5
  • 31
  • 37