1

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.

checho
  • 3,092
  • 3
  • 18
  • 30
  • I have exactly the same issue. If you find a workaround, please pass it along! – emoreau99 Apr 11 '16 at 12:15
  • 1
    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 http://www.pcreview.co.uk/threads/bug-in-folderbrowserdialog.2301005/ – emoreau99 Apr 11 '16 at 13:14
  • 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. – checho Apr 11 '16 at 17:58

3 Answers3

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;
    }
revebbever
  • 21
  • 3
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) 
RRR
  • 116
  • 1
  • 9
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.

CJRoffe
  • 31
  • 4