Monday, 28 January 2008

Howto set an InitialDirectory to FolderBrowserDialog

If you still don´t know, this might be useful:

The FolderBrowserDialog that comes boundled with .Net doesn´t have a InitialDirectory property. Maybe that´s why so many programs make us navigate through a hunderd folders again and again.

Well, making the FolderBrowserDialog to pre-select a folder is piece of cake. Just do the following:

this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
this.folderBrowserDialog1.SelectedPath = "C:\\ whateveryouwanthere \\";
this.folderBrowserDialog1.ShowDialog();

Easy as that!

I must say that it´s still kindof lazy when trying to resolve paths which include things like this:

@"c:\Windows\System\..\System32"

(Don´t look at me like that! this sometimes happens with automatically-generated paths... ;)

Don´t know why, but it seems that FolderBrowserDialog doesn´t re-use the same routines for parsing paths as the rest of the .Net. Some people say this is a bug... Anyway I guess it doen´t like the "\..\" thing.

An easy way to fix it, is to parse the "hot" path with the DirectoryInfo class. It is much stronger than the folderBrowser and can handle it:

System.IO.DirectoryInfo info = new DirectoryInfo(@"c:\Windows\System\..\System32" );
this.folderBrowserDialog1.SelectedPath = info.FullName;

And that´s it.

Hope it helped. Cheers!

13 comments:

Anonymous said...

In VS 2008, c#, this dont works. I search a lot on net to a solution for this and dont find nothing. The initial dir is always the value RootFolder, never a simple path like c:\dir. I think it's a bug, maybe in other versions can be done...

Iñaki Ayucar said...

Yes, it does work. Iam using it in VS2008, C#.

Which version of the .Net framework are you targeting your app in?

May be it´s because a small bug in the code I posted, that has been corrected. The bug was:

If using the "@" symbol before any string, a "\\" escape character will not be parsed. You have to either put the "@" before, or to use the "\\", but not both.

Was that the problem?

Anonymous said...

I'm using VS2005 and it does not work for me, just defaults to the root folder (in this example mycomputer)

Anonymous said...

please excuse the previous comment (the one about VS2005). it really works.

Iñaki Ayucar said...

Nevermind, that happens.

Cheers!

Anonymous said...

Steps to reproduce making this fail:

1) Set your FolderBrowserDialog.RootFolder to Environment.SpecialFolder.MyComputer;

2) Set the SelectedPath to a folder contained inside your 'My Documents' folder

3) Call FolderBrowserDialog.ShowDialog()

If you set FolderBrowserDialog.RootFolder to Environment.SpecialFolder.MyDocuments instead, the above steps work.

Iñaki Ayucar said...

Yes, that makes sense, though I haven´t tried.

I guess that´s because MyDocuments (or "Documents" in Vista) is below the Desktop folder, not below MyComputer.

Would setting the RootFolder to Desktop solve all this problems?

Anonymous said...

Not really. I need a generic solution to be able to present a folder list to a user - without worrying about whether the folder I start them from is contained within any of the other FolderBrowserDialog.RootFolder enumerations...

Think about the code necessary to determine if the starting folder (or parts of the starting folder) are contained in any of the other FolderBrowserDialog.RootFolder special folders. Because from what I see, you would have to check all of the enums that are logically below MyComputer on the file system.

Iñaki Ayucar said...

Well, if the FolderBrowser control is not enough for you, you can always make one for yourself. It´s very easy, using the TreeView control.

If you don´t want to do all that work, googling for "folder browser code project" shows several examples ready to use:

http://www.codeproject.com/KB/selection/folderbrowser.aspx

http://69.10.233.10/KB/dialog/ExtendedFolderBrowser.aspx

Hope it helps.

Anonymous said...

I found this to not work, and then realized that its case sensitive.

c://program files//

does not work.

C://Program Files//

does work.

hope this helps

-dBladez

jmcc said...

>>
Well, if the FolderBrowser control is not enough for you, you can always make one for yourself.
>>

Its not easy. I have tried it and its time-consuming, frustrating and still ended up with not many more features than the default.

Instead check out FolderView control from Shell MegaPack from http://www.ssware.com

Its highly customizable and its a control so it sits on your own form instead of showing a hideous modal dialog.

Junior.NET said...

I had to customize it and create a new member called InitialPath.

the code is at

http://mayhe.jr.googlepages.com/extendedfolderbrowser

Nelson said...

This article was a big help!
Thanks a lot!