Folder browser dialog like open file dialog
Asked Answered
S

5

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

Sambo answered 12/6, 2012 at 4:37 Comment(6)
See this question. Basically they seem to subclass the standard file open dialg.Squeak
@UweKeim: Where is the subclassing explained in that question thread? He has replied that he ended up using VistaBridgeSambo
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.Squeak
+1 for asking a question that actually got answered with the exact solution you requested.Schriever
As a solution I used the SaveFileDialog with the zero-width space symbol inserted as a filename. Then I just stripped off the path with Path.GetFullPath.Arlenarlena
Note: above is really hacky as you still leave the user with a Filename and Extension prompt.Jackie
E
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).

Estriol answered 11/2, 2015 at 8:1 Comment(0)
W
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.

Webbing answered 8/2, 2014 at 18:49 Comment(0)
G
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"
  };
Ghyll answered 3/3, 2018 at 18:33 Comment(0)
C
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# :)

Cordite answered 2/9, 2015 at 19:45 Comment(0)
B
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.

Benzyl answered 24/5, 2020 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.