The dialog will return incorrect SelectedPath when:
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.
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;
}
Can you open the dialog like this...
DialogResult result = openFileDialog1.ShowDialog();
and check the result before using the property SelectedPath
if(result == DialogResult.OK)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.