1

In my application I use both OpenFileDialog and FolderBrowserDialog on button click handlers:

var fileDialog = new System.Windows.Forms.OpenFileDialog();

var folderDialog = new System.Windows.Forms.FolderBrowserDialog();

Strange thing is that when calling OpenFileDialog it starts in explorer from folder in which file was chosen last time. But FolderBrowserDialog opens MyComputer each time in explorer no matter what folder was chosen last time. How can I get same behavior (remembering last chosen folder) for `FolderBrowserDialog'?

It's also interesting where 'OpenFileDialog' stores folder of last chosen file? Does windows stores it for each application?

Dork
  • 1,816
  • 9
  • 29
  • 57
  • Note that the settings for the InitialDirectory in the OpenFileDialog is wrong. This should be an actual path not an enum converted to a string – Steve Feb 23 '16 at 17:41

1 Answers1

5

You can set FolderBrowserDialog's selected folder using the SelectedPath property before opening:

var folderDialog = new System.Windows.Forms.FolderBrowserDialog();
folderDialog.RootFolder = System.Environment.SpecialFolder.MyComputer;
folderDialog.SelectedPath = <variable_where_you_stored_the_last_path>;

For example:

private string _lastFolderDialog = null;
// ...
var folderDialog = new System.Windows.Forms.FolderBrowserDialog();
folderDialog.SelectedPath = _lastFolderDialog;
if(folderDialog.ShowDialog() == DialogResult.OK)
{
  _lastFolderDialog = folderDialog.SelectedPath;
}

As for the OpenFileDialog, I think you mean:

fileDialog.InitialDirectory =
               Environment.GetFolderPath(System.Environment.SpecialFolder.MyComputer);

However that won't work, since MyComputer doesn't have a path. Try this instead:

fileDialog.InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"

You can check for other CLSIDs in the registry under HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CLSID

As you have already discovered, if InitialDirectory is set to null, it'll remember the last opened folder. This won't happen with FolderBrowserDialog though


All that said, and as I stated in the comments, the FolderBrowserDialog is pretty much obsolete and you should not use it at all. According to the MSDN for the native API (SHBrowseForFolder) that supports it:

For Windows Vista or later, it is recommended that you use IFileDialog with the FOS_PICKFOLDERS option rather than the SHBrowseForFolder function. This uses the Open Files dialog in pick folders mode and is the preferred implementation.

You may want to check this question (which in turn links to this page) or this other question on how to implement IFileDialog with FOS_PICKFOLDERS in .NET

Community
  • 1
  • 1
Jcl
  • 27,696
  • 5
  • 61
  • 92
  • regarding fileDialog - I deleted second line and it workd as I want (remembering last directory) by default. How can I get folderDialog work same way? – Dork Feb 23 '16 at 17:47
  • @Dork store the result of `folderDialog.SelectedPath` somewhere after calling `ShowDialog()`, and then set it before calling `ShowDialog` again. I don't think the `FolderBrowserDialog` does that by itself – Jcl Feb 23 '16 at 17:48
  • thx.. I thought they have same nature and should work similarly – Dork Feb 23 '16 at 17:50
  • @Dork nope, not actually... `OpenFileDialog` is in the Win32 API much prior to `SHBrowseForFolder` (which is what `FolderBrowserDialog` is made with). I'm not sure if there's a native implementation of `FOS_PICKFOLDERS`, but that's what you should be using: `FolderBrowserDialog` is obsolete and Microsoft doesn't recommend using it (since Vista on), despite many applications still using it (as a bad dialog as it is) – Jcl Feb 23 '16 at 17:54
  • 1
    Check [this question](http://stackoverflow.com/questions/600346/using-openfiledialog-for-directory-not-folderbrowserdialog) (and the accepted answer): this is the preferred method to browse for folders in Vista or later – Jcl Feb 23 '16 at 17:55