I’ve noticed what seems to be a bug in the browse for folder dialog:
- Open the new style browse for folder dialog that has a “New Folder” button. (E.g. FolderBrowserDialog class in .NET Framework, or SHBrowseForFolder in Win32 C++).
- Click the “Create New Folder” button to create a new folder.
- Type a name for the new folder. Do NOT press Enter or click anything to stop editing the new folder name after you are done typing it in.
- While the cursor is still blinking / the edit box is still active, click the OK button. You must click the OK button very quickly.
- Instead of returning the new name of the folder to the program, the dialog returns “New folder” directory. For example,
C:\New Folderinstead of what the user typed in, e.g.C:\My App Data Files.
My guess is that the dialog’s OK button reads the folder name from the treeview in the button’s “click/mouse-up” event. If you click it fast enough, the dialog reads the old value “New folder” since the treeview hasn’t had enough time to process the rename.
The end result is that the dialog returns the path to a directory that no longer exists (since it was renamed) and the application then fails.
I can reproduce this on my Windows 7 developer workstation, but it’s not limited to that computer. It’s been reproduced both in our production C++ application and a prototype C# application.
Has anyone else seen this and knows of a workaround / fix? This new-style dialog has been around since Windows 2000; I can’t believe this hasn’t been seen/fixed by now, but I can’t find the details! I know it can be worked around by the user by e.g. pressing Enter, waiting a moment, then clicking OK. But it seems that users with less computer experience don’t know that and it trips them up. So I’m looking for a programmatic workaround / fix.
I recommend setting your ambitions low, since you’re effectively trying to bugfix the OS itself at this point.
Consider checking if
GetFileAttributes(folderName) == INVALID_FILE_ATTRIBUTESafter the dialog exits.If that expression is true, present the dialog again. That will at least prevent crashes or other failure scenarios.