locked
FolderBrowserDialog bug when click OK while modify new folder's name RRS feed

  • Question

  • Hi!

    I make a New Folder in the FolderBrowserDialog, firstly the New Folder's name is selected automatically for editing. I edited it to MyDirectory, but instead of hit ENTER then click OK, I click to OK button while I'm in edit mode. After this, the SelectedPath of my FolderBorwserDialog is pointing to New Folder, but the name of this folder is already changed to MyDirectory.

    FolderBrowserDialog fbd = new FolderBrowserDialog();

    if (fbd.ShowDialog() == DialogResult.OK)

    {

    var path = fbd.SelectedPath;

    }

    Is this a bug?


    • Edited by koviroli Tuesday, February 20, 2018 1:00 PM
    Tuesday, February 20, 2018 1:00 PM

All replies

  • Seems like an inconvenience. However, it is not always reproducible.

    Maybe use a modern dialog, which looks like Open File dialog, but is configured to select folders only. (The File àOpen àFolder menu in Visual Studio 2017 is an example of such dialog). Unfortunately, seems that it is not available yet in standard libraries.

    If you are interested, search for IFileDialog, FOS_PICKFOLDERS, WindowsAPICodePack.


    • Edited by Viorel_MVP Wednesday, February 21, 2018 6:59 PM
    Wednesday, February 21, 2018 6:57 PM
  • If I remember correctly, FolderBrowserDialog will look for presence of IFileDialog first, and use SHBrowseForFolder() only if that is not found.

    Btw, I think this behavior is quite logical. The "New Folder" name is the name of that Shell Item when the dialog is closing (hence the name be passed to .NET runtime), and when the folder name is actually changed, the dialog is no longer there to receive the notification.

    To fix it would require the dialog to penetrate the "Shell" provided UI to save the Shell Item's ID before closing, then wait a brief moment after the dialog is closed, use the ID to query the shell item's name again, then finally pass to .NET runtime. I'm not even sure if that's possible or not, and it certainly won't work well on the other platforms like Linux or Mac.

    I think you should accept the behavior "as is".

    Thursday, February 22, 2018 1:19 AM
    Answerer
  • Hi koviroli,

    According to your description, do you put the code you provided together inside a method? 

    If yes, please put the code below outside the event. I use a button click event for reference.

    FolderBrowserDialog fbd = new FolderBrowserDialog();
    If you want to make the folder point to the selected one, you could try the code below.


      FolderBrowserDialog fbd = new FolderBrowserDialog();
            private void button1_Click(object sender, EventArgs e)
            {
    
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    fbd.SelectedPath = fbd.SelectedPath;
                }
            }

    Best Regards,

    Wendy


    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.



    Wednesday, February 28, 2018 5:20 AM
  • No. He is asking for a specific scenario:

    1) Open a FolderBrowserDialog.

    2) Try add a new folder there. A new item named "New Folder" is added and put into edit mode, so you can enter a new folder name there.

    3) Change the folder name, but without pressing enter to confirm the name change, just press OK button.

    Net result: the dialog returns "New Folder" is selected, but in fact it's already been renamed to the new folder name you provided, so any operation you intend to do with the folder fails.

    Thursday, March 1, 2018 1:23 AM
    Answerer
  • Hi,

    @cheng00, I misunderstood something. Thanks for your reminding. 

    @koviroli, I test the code with different scenarios. If we create new folder and change name directory in desktop or the same level folder like desktop like user folder "C:\Users\v-wezan", it would not get the MyDirectory from the SelectedPath. It will get the default value of New Folder.

    If we create a folder under this level, like create a folder under a folder on desktop, it could get the SelectedPath of MyDirectory.

    Best Regards,

    Wendy


    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, March 1, 2018 6:26 AM
  • I faced the same problem and got around in this way:

            Try
                dlgFolderBrowse = New FolderBrowserDialog()
                dlgFolderBrowse.SelectedPath = GetInitialLinkDirectory()
                Dim dlgResult As DialogResult = DialogResult.OK
                Dim strSelectedPath As String = ""
                Do While dlgResult = DialogResult.OK And Not IO.Directory.Exists(strSelectedPath)
                    dlgResult = dlgFolderBrowse.ShowDialog()
                    strSelectedPath = dlgFolderBrowse.SelectedPath
                    If dlgResult = DialogResult.OK AndAlso IO.Directory.Exists(dlgFolderBrowse.SelectedPath) Then
                        Return strSelectedPath
                    Else
                        MsgBox($"The folder '{strSelectedPath}' was not found. This may have been renamed, please try to reselect it.",
                               MsgBoxStyle.Exclamation, Me.ParentForm.Text)
                        'select parent path
                        dlgFolderBrowse.SelectedPath = Strings.Left(strSelectedPath, InStrRev(strSelectedPath, "\") - 1)
                    End If
                Loop
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                dlgFolderBrowse.Dispose()
                dlgFolderBrowse = Nothing
            End Try
    It doesn't solve the problem but makes the user aware it has been renamed and sends them back to the selection form. This also works when they rename an existing forlder and click OK before coming out of edit mode


    Thursday, January 10, 2019 6:38 AM