Bizarre FolderBrowserDialog behaviour
Asked Answered
W

3

9

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...

Woolly answered 2/4, 2012 at 8:27 Comment(9)
I tried your code with .NET 4 and 3.5 I didnt see any problem. IF you dont provide RootFolder it will browse anything...Desktop golder it self is not expandable. Would be nice to see some screenshots from your application.Banas
That's the problem, you can't browse at all with the default RootFolder. Sure, I'll grab some of the expected and current behaviour.Woolly
Have you created a class yourself with the name FolderBrowserDialog?Outdare
Nope, intellisense displays as System.Windows.Forms.FolderBrowserDialog.Woolly
do you have any wndproc overrides somewhere in your code? do you do any WM_* message handling at all in your application? Is it possible to scale of parts of your program to isolate the issue?Outdare
Actually, I believe we do have a fair few overrides and message handling like that in some places. Unfortunately, it's not easy to scale it back, it's a huge (~1mil SLOC) tightly coupled application... Is there anything in particular I should look at in these methods?Woolly
Do you have STAThread as an attribte for your Main class?!Banas
@Sean87 Yes, it's running in the STA.Woolly
Although this may not be very helpful, this person appears to have the same unresolved issue.Prig
R
1

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.

Rachmaninoff answered 29/11, 2012 at 13:1 Comment(0)
M
0

If you're only accessing the users private folders, use

browsePath.RootFolder = Environment.SpecialFolder.Personal

Only the specified folder and any subfolders that are beneath it will appear in the dialog box and be selectable. The SelectedPath property, along with RootFolder, determines what the selected folder will be when the dialog box is displayed, as long as SelectedPath is an absolute path that is a subfolder of RootFolder (or more accurately, points to a subfolder of the shell namespace represented by RootFolder).

In short, you can not input someones private folder as a startup selectedPath unless RootFolder is inside the current users private folder already.

For details, see: http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.rootfolder.aspx

Musick answered 3/4, 2012 at 8:1 Comment(5)
I'm pretty sure C:\Users\Blah is beneath the My Computer folder though? I think you've got that second bit the wrong way round too: you can only input a selectedPath that is below the RootFolder, not the other way around... It's also not possible to restrict to just the user's directory, they'll need to export to network and removable drives too.Woolly
Sorry, what I mean is "RootFolder is set to the current users private folder".Musick
However, I got it to work if you omit setting the RootFolder in the first place. :)Musick
Yes, that may work, but we need to be able to select network drives, so it's not an option. Also, omitting RootFolder defaults to the Desktop folder, which as I mentioned doesn't work at all!Woolly
Heh, sorry, the whole application is marked for the STA... Thanks anyway though!Woolly
C
0

VB.NET code

Dim fdb As New FolderBrowserDialog
    With fdb
        '.RootFolder = Environment.SpecialFolder.MyComputer
        'this folder don't exists in vista, the my computer folder was renamed to computer (in spanish "mi pc" to "equipo")
        'try with another initial folder
        .RootFolder = Environment.SpecialFolder.Desktop
        'You can set the desktop as home directory because users typically already have shortcuts or the left side menu to navigate
        Dim dr As DialogResult = .ShowDialog
        If _
            dr = DialogResult.OK Or _
            dr = DialogResult.Yes Then _
            If IO.Directory.Exists(.SelectedPath) = True Then _
            Me.textBoxTo.Text = UpdateLocation(.SelectedPath)
    End With

basically, try another directory and make sure the chosen directory exists. if you're still having problems, is probably due to some fault in the system.

Caesarism answered 21/4, 2012 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.