FolderBrowserDialog shows incorrect SelectedPath after new folder is being created and renamed
Asked Answered
S

3

2

The dialog will return incorrect SelectedPath when:

  1. Once shown, click the New Folder button
  2. Type in some name for the new folder
  3. Click OK, **without pressing ente

Code used:

   FolderBrowserDialog dialog = new FolderBrowserDialog();
   dialog.ShowDialog();
   Console.WriteLine(dialog.SelectedPath);

Any suggestions how to overcome this and get the correct path for the renamed new folder?

UPDATE I have tested this on Windows 7, 8.1 and 10. It is reproducible on 7 and 10, while in 8.1 it seems to work correctly.

Swineherd answered 11/4, 2016 at 6:57 Comment(3)
I have exactly the same issue. If you find a workaround, please pass it along!Leastways
I have googled around and found a workaround. The guy check if SelectedPath is existing and if not, loop to find the newest folder in the same path. Not an ideal solution but at least it works! Check for the comment of Scott PRD of Oct. 6 2015 from pcreview.co.uk/threads/bug-in-folderbrowserdialog.2301005Leastways
I don't think this is realiable solution. What happens if another process copies at the same time. Things will get messy. Anyway thanks for the information.Swineherd
A
2

I wrote the suggested workaround from emoreau99. Ugly but it works.

    public string GetPathFromFolderBrowserDialog()
    {
        var path = "";
        var fbd = new FolderBrowserDialog();

        if (fbd.ShowDialog() == DialogResult.OK)
        {
            path = fbd.SelectedPath;

            if (!Directory.Exists(path))
            {
                var lastCreatedDir = new DirectoryInfo(path).Parent
                    .GetDirectories()
                    .OrderByDescending(d => d.LastWriteTimeUtc)
                    .First();

                path = lastCreatedDir.FullName;
            }
        }

        return path;
    }
Audrit answered 26/2, 2021 at 14:55 Comment(1)
more workaround than solution, but works pretty fine for my environment.Snafu
T
0

Can you open the dialog like this...

DialogResult result = openFileDialog1.ShowDialog();

and check the result before using the property SelectedPath

if(result == DialogResult.OK) 
Treasure answered 11/4, 2016 at 7:8 Comment(1)
I did and it is the same.Swineherd
S
0

I found the correct answer here: Problem with FolderBrowserDialog

If you explicitly set the property .ShowNewFolderButton = True, then .SelectedPath will correctly return the updated new folder name.

It's an odd one, because .ShowNewFolderButton is True by default, but this solution fixed the problem for me.

Sloganeer answered 18/3, 2022 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.