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!