Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bizarre FolderBrowserDialog behaviour

I'm supporting an old version of a C# application, running on .NET 3.5. We've found an issue with the FolderBrowserDialog on Windows Vista (either 32 or 64-bit).

Basically what happened is that the dialog would appear, but only the root Desktop node would be shown, not even able to expand it to show anything else. Obviously, that's impossible to use.

After a huge amount of trial and error I eventually managed to get something useable, by setting the RootFolder property before the rest of the setup:

FolderBrowserDialog browsePath = new FolderBrowserDialog();
browsePath.RootFolder = Environment.SpecialFolder.MyComputer;
browsePath.SelectedPath = this.textBoxTo.Text;
browsePath.Description = TextResources.OutputTargetCaption;
browsePath.ShowNewFolderButton = true;

if(browsePath.ShowDialog(this) == DialogResult.OK)
{
    this.textBoxTo.Text = UpdateLocation(browsePath.SelectedPath);
}

This almost works; however, I've got the bizarre issue that then the SelectedPath (by definition the contents of textBoxTo) is a path to within the current user's home directory, it won't automatically browse to that path, instead just showing the My Computer node expanded to one level. It's perfectly fine for any other path.

I'm sure your first guess would be a permissions issue, as was my intuition. It doesn't appear to be, this issue occurs running normally and as an Administrator, for both standard and Administrator accounts. It's a clean install, of course, no weird permissions or anything.

This is pretty annoying when all of our defaults are within the current user's directory!

Note: This only happens within the application; it's not reproducible with a small test application, as far as I've seen.

Any ideas on what could be causing this?

Update: Screenies:
This is the behaviour I want (obtainted from a little test app)
This is what I get with the default property
This is what I get by setting the root to My Computer Note: The last image had the same SelectedPath set as the expected image...

like image 421
Alexander R Avatar asked Apr 02 '12 08:04

Alexander R


1 Answers

I had a similar problem. In Windows Vista and Windows 7 the following code:

browsePath.RootFolder = Environment.SpecialFolder.MyComputer;

returns the Desktop. If you look in Windows Explorer, the root of the tree is Desktop, not My Computer like it was in Windows XP. To solve this problem use this instead:

browsePath.RootFolder = @"C:\";

Every Windows computer has a C:\ drive, so this will solve your problem.

I hope this helps you.

like image 86
Alexandru Dicu Avatar answered Oct 11 '22 11:10

Alexandru Dicu