none
FolderBrowseDialog does not display directory tree RRS feed

  • Question

  • I am trying to use a FolderBrowserDialog to allow a user to select a working folder. But when I display the dialog there is no directory tree, just the root directory. It does not open up to display the sub-directories within it. The selected path is always the same as the initial root folder. My code:

    Sub TestIt()

    Dim dlgChooseFolder As New FolderBrowserDialog

    Dim dr As DialogResult

    dlgchoosefolder.Description = _

    "Select the directory that you want to use As the default."

    ' Default to the My Documents folder.

    dlgChooseFolder.RootFolder = Environment.SpecialFolder.Personal

    dr = dlgChooseFolder.ShowDialog

    If (dr = DialogResult.OK) Then

    Debug.Print("dr = OK" & vbCrLf & dlgChooseFolder.SelectedPath)

    Else

    Debug.Print("dr <> OK")

    End If

    End Sub

    The dialog displays, but the "Documents" icon does not open. The SelectedPath property never changes. Can anyone help? What am I doing wrong?

    Thursday, June 15, 2017 4:45 AM

Answers

  • .RootFolder = Environment.SpecialFolder.MyDocuments and .RootFolder = Environment.SpecialFolder.Personal both yield the same result. I am using Windows 10. That may be an issue.

    Have you considered making your own?

    It might prove to be your best bet with this.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by MableMaud Friday, June 16, 2017 4:17 AM
    Thursday, June 15, 2017 7:53 PM

All replies

  • Hi

    Try replacing

    dlgChooseFolder.RootFolder = Environment.SpecialFolder.Personal

    with

    dlgChooseFolder.SelectedPath = Environment.SpecialFolder.Personal.ToString

    (BTW, when posting code, please use the Code Block tool in the ToolBar)


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, June 15, 2017 5:25 AM
    Thursday, June 15, 2017 5:25 AM
  • The dialog displays, but the "Documents" icon does not open. The SelectedPath property never changes. Can anyone help? What am I doing wrong?

    The root folder is probably not suitable for this usage.  Use the SelectedPath property instead:

    The problem appears to be related to virtual folders.  The alternative is to construct your own folder browser dialog.

    Thursday, June 15, 2017 5:34 AM
  • Hi MableMaud,

    I use the code below that can display directory tree.

    Dim folderBrowserDialog1 As New FolderBrowserDialog
    
            folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer
            folderBrowserDialog1.SelectedPath = "D:\"
            If folderBrowserDialog1.ShowDialog = DialogResult.OK Then
                MsgBox(folderBrowserDialog1.SelectedPath)
            End If

    Here's a way to use the OpenFileDialog, far from perfect but better than folderBrowserDialog.

    Dim myStream As Stream = Nothing
        Dim openFileDialog1 As New OpenFileDialog()
    
        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True
    
        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Try
                myStream = openFileDialog1.OpenFile()
                If (myStream IsNot Nothing) Then
                    ' Insert code to read the stream here.
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
            Finally
                ' Check this again, since we need to make sure we didn't throw an exception on open.
                If (myStream IsNot Nothing) Then
                    myStream.Close()
                End If
            End Try
        End If
    

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 15, 2017 6:59 AM
    Moderator
  • This helps, but is not quite right. After making the change you suggest, I get a usable directory tree, but the RootFolder is "Desktop", not "MyDocuments". The SelectedPath (before the ShowDialog command) is "MyDocuments". I can drill down the tree and select the directory I want, and after clicking OK the SelectedPath is as expected. My original code came directly from the Microsoft help file. According to the documentation, the RootFolder must be a special folder, not a string. Is there no way to change the default?
    Thursday, June 15, 2017 5:02 PM
  • Leaving RootFolder as its default value displays a usable directory tree, but it starts at the "Desktop". My code came directly from the Microsoft help file. Is there no way to change the default value of RootFolder?
    Thursday, June 15, 2017 5:08 PM
  • Hi

    Which folder do you want it to start with? I just used what you originally posted.


    Regards Les, Livingston, Scotland

    Thursday, June 15, 2017 5:08 PM
  • Is there no way to change the default value of RootFolder?

    Yes, but it has to be one of these:

    https://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx

    You're better off to use the .SelectedPath as others have said. Then you have full control.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, June 15, 2017 5:14 PM
  • I want the user to select a directory, not a file. I do not see any way to do this with the OpenFile Dialog.
    Thursday, June 15, 2017 5:32 PM
  • As in my original post, I want dlgChooseFolder.RootFolder = Environment.SpecialFolder.Personal
    Thursday, June 15, 2017 5:35 PM
  • I want the user to select a directory, not a file. I do not see any way to do this with the OpenFile Dialog.

    I'm not sure who you're talking to but I thought this was about a FolderBrowserDialog, not an OpenFileDialog.

    Did I misunderstand?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, June 15, 2017 5:36 PM
  • I am trying to use dlgChooseFolder.RootFolder = Environment.SpecialFolder.Personal. Why doesn't this set the RootFolder to "My Document"? Setting the SelectedPath property allows the user to move up the directory tree to places that could be undesirable.
    Thursday, June 15, 2017 5:39 PM
  • As in my original post, I want dlgChooseFolder.RootFolder = Environment.SpecialFolder.Personal


            Using fbd As New FolderBrowserDialog
                With fbd
                    .Description = "Select The Folder"
                    .RootFolder = Environment.SpecialFolder.Personal
                    .ShowNewFolderButton = False
                End With
    
                If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
                    ' Now look at fbd.SelectedPath
                End If
            End Using

    Try that please?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, June 15, 2017 5:40 PM
  • I am trying to use dlgChooseFolder.RootFolder = Environment.SpecialFolder.Personal. Why doesn't this set the RootFolder to "My Document"? Setting the SelectedPath property allows the user to move up the directory tree to places that could be undesirable.

    I don't know who you're talking to (we don't all see this forum the same way) but with what I posted, mine does.

    It might be O/S specific though.

    ***** EDIT *****

    You might try to force it:

    .RootFolder = Environment.SpecialFolder.MyDocuments

    Does that work?


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Thursday, June 15, 2017 5:44 PM
  • You are correct. Another responder suggested using OpenFileDialog.
    Thursday, June 15, 2017 6:35 PM
  • I have tried both True and False for ShowNewFolderButton. Makes no difference. I begin to suspect a Windows 10 issue. My code worked a few years ago. It may have been before I "upgraded" to 10.
    Thursday, June 15, 2017 6:38 PM
  • .RootFolder = Environment.SpecialFolder.MyDocuments and .RootFolder = Environment.SpecialFolder.Personal both yield the same result. I am using Windows 10. That may be an issue.
    Thursday, June 15, 2017 6:41 PM
  • .RootFolder = Environment.SpecialFolder.MyDocuments and .RootFolder = Environment.SpecialFolder.Personal both yield the same result. I am using Windows 10. That may be an issue.

    You may very well be onto it.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, June 15, 2017 6:43 PM
  • .RootFolder = Environment.SpecialFolder.MyDocuments and .RootFolder = Environment.SpecialFolder.Personal both yield the same result. I am using Windows 10. That may be an issue.

    Have you considered making your own?

    It might prove to be your best bet with this.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by MableMaud Friday, June 16, 2017 4:17 AM
    Thursday, June 15, 2017 7:53 PM
  • .RootFolder = Environment.SpecialFolder.MyDocuments and .RootFolder = Environment.SpecialFolder.Personal both yield the same result. I am using Windows 10. That may be an issue.

    With the FolderBrowserDialog you have the option of opening it in the folder you want but rooted in some higher-level folder. That's what SelectedPath will do for you.

    With your custom-built folder browser you can configure it any way you want.  That would be my preferred option.

    Your third choice is to search the usual sites for a version that has already been upgraded to be fully compliant with current operating systems.  

    Thursday, June 15, 2017 10:22 PM