0

I am using FolderBrowserDialog in my application.Through it's constructor I can set RootPath, SelectedPath. It should always open with D:\Export\ directory as default path. If the user selects any other path, the newly selected directory should be reflected on folder.SelectedPath variable also if the user closes the dialog window and open it again, it should open with last time selcted folder(User selected folder). It should not open the default folder (D:\Export).

public void OpenFolderDialog()
{
FolderBrowserDialog folder = new FolderBrowserDialog(Environment.SpecialFolder.MyComputer, @"D:\Export");
folder.ShowDialog();

    if(!string.IsNullOrEmpty(folderBrowserDialog.SelectedPath) && Directory.Exists(folderBrowserDialog.SelectedPath))
    {
        ExportData(folderBrowserDialog.SelectedPath);
    }
    else
    {
        if (string.IsNullOrEmpty(folderBrowserDialog.SelectedPath))
        {
          log.WarningMsg("FolderBrowserDialog selected path is empty");
         }
         else
         {
           log.WarningMsg("FolderBrowserDialog selected Directory Does not exist");
         }
      }
 }

Note : folderBrowserDialog.SelectedPath is readonly property. we cant able to assign any values in it.

How can we remember the Last Selected Folder path?

Suryakavitha
  • 1,371
  • 5
  • 33
  • 49
  • 3
    Why won't you create a (static) string variable that will be assigned the value of the selected folder once the `FolderBrowserDialog` closes successfully and use that variable when opening/creating the dialog again? – Mike May 19 '20 at 13:46
  • 1
    What FolderBrowserDialog is that? The standard one doesn't have those constructors. Anyway, you can store the User choice in a Project's setting, so it will persist. – Jimi May 19 '20 at 14:01
  • @Jimi - It is custom FolderBrowserDialog. But still it has all available properties along with that. It is implemented on top of actual FolderBrowserDialog control. – Suryakavitha May 19 '20 at 15:37
  • @mm8 - I got it. Thanks. – Suryakavitha May 19 '20 at 15:52

2 Answers2

2

You could store the value of the SelectedPath property in a variable as soon as the ShowDialog() method returns:

private string _selectedPath = @"D:\Export\"; // <-- the default value
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    FolderBrowserDialog dialog = new FolderBrowserDialog()
    {
        SelectedPath = _selectedPath
    };
    dialog.ShowDialog();
    _selectedPath = dialog.SelectedPath;
}
mm8
  • 163,881
  • 10
  • 57
  • 88
0

If you want to use System.Windows.Forms.FolderBrowserDialog, you're going to have to store the SelectedPath value in a field or property, then set SelectedPath back to that value on subsequent calls before calling ShowDialog again (as shown in mm8's answer).

But honestly, I wouldn't bother with that. To me, the real answer is not to use FolderBrowserDialog at all. That dialog is very unfriendly from the user's point of view. As recommend in this answer to the question How to use OpenFileDialog to select a folder?, I too recommend using Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog. It's part of a Microsoft NuGet package that can be found here: https://www.nuget.org/packages/Microsoft.WindowsAPICodePack-Shell.

This dialog automatically does what you are trying to do: it remembers the last folder the user selected and starts in that folder the next time to user opens the dialog- no additional code required.

Example usage:

var dialog = new Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog { IsFolderPicker = true };
if (dialog.ShowDialog() == Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogResult.Ok)
{
    string selected = dialog.FileName;
}
Keith Stein
  • 6,235
  • 4
  • 17
  • 36
  • 2
    As a note, from .Net Core 3.0, the Vista style FolderBrowserDialog is [now the default](https://learn.microsoft.com/en-us/dotnet/core/compatibility/fx-core#modernization-of-the-folderbrowserdialog) – Jimi May 19 '20 at 14:23
  • I want to supply the folder it starts in. If somebody recently used notepad I DON'T want the last folder they used, why on earth would I – AriesConnolly Jan 01 '23 at 16:34