20

Please see the snapshot below. This was taken from "New project creation" workflow in Visual Studio 2008.

This window is used for selecting a folder in which the project will be stored. How do I create a similar window in my c# application?

enter image description here

moribvndvs
  • 42,191
  • 11
  • 135
  • 149
Rockstart
  • 2,337
  • 5
  • 30
  • 59
  • 1
    See [this question](http://stackoverflow.com/questions/1250991/visual-studio-2008-folder-browser-dialog?rq=1). Basically they seem to subclass the standard file open dialg. – Uwe Keim Jun 12 '12 at 04:51
  • @UweKeim: Where is the subclassing explained in that question thread? He has replied that he ended up using VistaBridge – Rockstart Jun 12 '12 at 05:14
  • 1
    Some time back I downloaded VistaBride and examined the sources on how they did it. IIRC it was subclassing of the standard file open dialog. – Uwe Keim Jun 12 '12 at 05:19
  • +1 for asking a question that actually got answered with the exact solution you requested. – Alex Essilfie Jun 09 '13 at 14:46
  • As a solution I used the `SaveFileDialog` with the [zero-width space symbol](http://www.fileformat.info/info/unicode/char/200B/index.htm) inserted as a filename. Then I just stripped off the path with `Path.GetFullPath`. – Hi-Angel Aug 19 '15 at 13:48
  • Note: above is really hacky as you still leave the user with a Filename and Extension prompt. – stigzler Apr 24 '23 at 16:04

5 Answers5

6

It is something similar in Office, a dialog which allows to select a folder. The only difference is that the Select folder button is named "OK" instead of "Select folder".

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Core.FileDialog fileDialog = app.get_FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFolderPicker);
fileDialog.InitialFileName = "c:\\Temp\\"; //something you want
int nres = fileDialog.Show();
if (nres == -1) //ok
{
    Microsoft.Office.Core.FileDialogSelectedItems selectedItems = fileDialog.SelectedItems;

    string[] selectedFolders = selectedItems.Cast<string>().ToArray();

    if (selectedFolders.Length > 0)
    {
        string selectedFolder = selectedFolders[0];
    }
}

Of course, you need to add references to Microsoft.Office.Core (Microsoft Office 14.0 Object Library) and Microsoft.Office.Interop.Excel (Microsoft Excel 14.0 Object Library).

daniel
  • 157
  • 1
  • 1
3

I found a good article about the default FolderBrowserDialog and its limitations: http://www.ssware.com/articles/folderbrowserdialog-unmasked-everything-you-wanted-to-know-about-the-folder-browser-component-from-dotnet-framework.htm

There is a third party compoment "Shell MegaPack" (http://www.ssware.com/megapack.htm) from ssware which offers windows explorer like file and folder browser-controls for WinForms, ASP.net and WPF.

guppy81
  • 91
  • 1
  • 4
2

If you are ok with adding a nuget package, Microsoft.WindowsAPICodePack.Shell has a CommonOpenFileDialog that can be used in "folder mode" which should match your desired usage.

var directoryDialog = new CommonOpenFileDialog
  {
     IsFolderPicker = true,
     Title = "Select Folder"
  };
BimlJake
  • 21
  • 2
0

I modified the code from C# to VB, and my env is VS2015 + Office 2010. My code is slightly different than Daniel's, as some function from Daniel's code supports to only Office 2003/2007

By using a new excel instance, it will be slower than just opening a OpenFileDialog or OpenFolderDialog, but is it way more user friendly. My program is only calling this code once, so trading off the performance for user-friendliness is not a concern in my case.

Imports Microsoft.Office
Imports Excel = Microsoft.Office.Interop.Excel

Private Sub Button_select_raw_dir_Click(sender As Object, e As EventArgs) Handles Button_select_raw_dir.Click
    Dim raw_app As Excel.Application = New Excel.Application
    Dim raw_data_open_folder_dialog As Microsoft.Office.Core.FileDialog
    raw_data_open_folder_dialog = raw_app.FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFolderPicker)
    raw_data_open_folder_dialog.AllowMultiSelect = False
    raw_data_open_folder_dialog.Title = "Please select the raw data's dir "
    Dim nres As Integer = raw_data_open_folder_dialog.Show()
    Dim sz_SelectedPath As String = Nothing
    If nres = -1 Then '-1 means open... lol
        For Each selectedItems As Object In raw_data_open_folder_dialog.SelectedItems
            sz_SelectedPath = selectedItems.ToString()
        Next
        TextBox_raw_data_dir.Text = sz_SelectedPath
    End If

    raw_app.Quit()
    ReleaseComObject(raw_app)
    GC.Collect()
    GC.WaitForPendingFinalizers()
End Sub

' Release excel objects to avoid memory leak
Public Sub ReleaseComObject(ByRef obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
        MsgBox("Exception! Failed to release com obj, debug your code.")
    End Try
End Sub

If you want a C# version, I believe you are smart enough to port it to C# :)

Joe
  • 841
  • 2
  • 10
  • 25
0

Kindly check out BetterFolderBrowser. It provides just what you need and so much more.

BetterFolderBrowser is a .NET component library that was written to help developers provide a better folder-browsing and selection experience to users by employing a similar browser dialog as the standard OpenFileDialog in place of the current FolderBrowserDialog which only allows for single-folder selections with its tree-view display format. This allows for a much easier viewing, modification, searching and selection experience using the standard Windows Explorer dialog.

Willy Kimura
  • 319
  • 2
  • 6