Search code examples
c#wpfookii

Folder Browser Dialog Inital Folder


I was looking for and Open Folder Dialog (wpf). I get the Ookii dialogs for wpf and I use VistaFolderBrowserDialog. (I don't like the FolderBrowserDialog of Windows Forms).

I save the "last open folder". So the next time the user open this VistaFolderBrowserDialog the Initial Folder is the "last one" I saved.

...
//Save the new actual folder
MyProject.ProgramConfigurationFile.Instance.OpenFolderPath = System.IO.Path.GetDirectoryName(folderDialog.SelectedPath);

VistaFolderBrowserDialog has the property => RootFolder:

public Environment..::..SpecialFolder RootFolder { get; set; } But it is a SpecialFolder type.

So I am looking for a way to set my String OpenFolderPath to the RootFolder property.

VistaFolderBrowserDialog folderDialog = new VistaFolderBrowserDialog();
folderDialog.Description = "Please select the folder";
folderDialog.UseDescriptionForTitle = true;
if ((bool)folderDialog.ShowDialog(this))
{
     //Get the last open folder saved (if exist).
     if(!String.IsNullOrEmpty(MyProject.ProgramConfigurationFile.Instance.OpenFolderPath))
     {
         folderDialog.RootFolder = Environment.SpecialFolder. //I would like to set OpenFolderPath
     }  
}

Other Folder Browsers are also welcome.

Thank you very much.


Solution

  • I did not know before now Ookii dialogs, but after a little search and knowing how common Open Folder Dialog works i suggest that you set your lastOpenFolder to the SelectedPath property

    folderDialog.SelectedPath = MyProject.ProgramConfigurationFile.Instance.OpenFolderPath;
    

    But this must be done before folderDialog.ShowDialog(this) showing the dialog.

    So it should look something like

    VistaFolderBrowserDialog folderDialog = new VistaFolderBrowserDialog();
    folderDialog.Description = "Please select the folder";
    folderDialog.UseDescriptionForTitle = true;
    
    if(!string.IsNullOrEmpty(MyProject.ProgramConfigurationFile.Instance.OpenFolderPath))
        folderDialog.SelectedPath = myProject.ProgramConfigurationFile.Instance.OpenFolderPath;
    
    if ((bool)folderDialog.ShowDialog(this))
    {
        string newSelectedFolderPath = folderDialog.SelectedPath;
        // Use new folderPath
    }
    

    Let me know if this solves it.

    I hope I was usefull.