10

I thought using application settings would do the trick but I'm not getting it to work. This is what I have:

private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (fbFolderBrowser.ShowDialog() == DialogResult.OK)
            {
                // I want to open the last folder selected by the user here.
            }

When the user clicks on this button, I want to open the browse window to the last folder he accessed and save it. Next time he clicks on the button, it'll automatically select that folder.

I was thinking maybe I could use user variables where I can change at run-time but I'm not getting it to work. Can anyone give me a hand?

1
  • 2
    Also, if you're using Application Settings, don't forget to Save your changes: Settings.Default.Save();
    – jay_t55
    Oct 11, 2012 at 14:40

5 Answers 5

19

Go to Settings Page, Project Designer of the project which you have created and add folder path variable inside the application. Now add below code to restore the last selected folder path.

FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
folderBrowser.Description = "Select a folder to extract to:";
folderBrowser.ShowNewFolderButton = true;
folderBrowser.SelectedPath = Properties.Settings.Default.Folder_Path;
//folderBrowser.SelectedPath = project_name.Properties.Settings.Default.Folder_Path;

if (folderBrowser.ShowDialog() == DialogResult.OK)
{

    if (!String.IsNullOrEmpty(Properties.Settings.Default.Folder_Path))
        Properties.Settings.Default.Folder_Path = folderBrowser.SelectedPath;

    Properties.Settings.Default.Folder_Path = folderBrowser.SelectedPath;
    Properties.Settings.Default.Save();
}
2
4

There are two places where you can find the last folder accessed by a user:

  1. Recent Files and Folders: It can be found here: C:\Documents and Settings\USER\Recent
  2. Registry: In the registry to look here: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU

You can use this snippet to find it:

public static string GetLastOpenSaveFile(string extention)
{
    RegistryKey regKey = Registry.CurrentUser;
    string lastUsedFolder = string.Empty;
    regKey = regKey.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU");

    if (string.IsNullOrEmpty(extention))
        extention = "html";

    RegistryKey myKey = regKey.OpenSubKey(extention);

    if (myKey == null && regKey.GetSubKeyNames().Length > 0)
        myKey = regKey.OpenSubKey(regKey.GetSubKeyNames()[regKey.GetSubKeyNames().Length - 2]);

    if (myKey != null)
    {
        string[] names = myKey.GetValueNames();
        if (names != null && names.Length > 0)
        {
            lastUsedFolder = (string)myKey.GetValue(names[names.Length - 2]);
        }
    }

    return lastUsedFolder;
}

OR

In windows XP when you press Save on a SaveFileDialog the directory where the file is saved, is set as the new current working directory (the one in Environment.CurrentDirectory).

In this way, when you reopen the FileDialog, it is opened on the same directory as before.

By setting FileDialog.RestoreDirectory = true, when you close the FileDialog the original working directory is restored.

In Windows Vista/Seven the behavior is always as FileDialog.RestoreDirectory = true.

4
  • geeze! i was thinking something a lot simpler, such as keeping track of the last folder using the project properties variables. lol.
    – JJ.
    Aug 20, 2012 at 14:26
  • It also depends on the OS you are working on. It works differently in different OSs. Aug 20, 2012 at 14:32
  • this is for a machine using windows 7. though this could really vary :S
    – JJ.
    Aug 20, 2012 at 14:32
  • 1
    RegistryKeys are IDisposable, be sure to wrap them in a using block or call .Dispose() on them when you are done. Apr 15, 2016 at 14:27
2

Application settings can do the trick.
A more elaborated version is here

use a Setting of type string

create a setting for each button and store the Path there. Then use the setting as the ofd.InitialPath

using the above code example, try this:

right click your app name in Solution Explorer, click on the Settings tab Name = Button1Path Type = String Scope = User

then use this:

private void btnBrowse_Click(object sender, EventArgs e)
{
    fbFolderBrowser.InitialDirectory=this.Settings.Button1Path;
    if (fbFolderBrowser.ShowDialog() == DialogResult.OK)
    {
        // I want to open the last folder selected by the user here.
        this.Settings.Button1Path=fbFolderBrowser.SelectedPath
    }
}
0

You can easily keep track of your last-selected folder, like this:

public String LastSelectedFolder;

private void btnBrowse_Click(object sender, EventArgs e)
{
    fbFolderBrowser.InitialDirectory=this.Settings.Button1Path;
    if (fbFolderBrowser.ShowDialog() == DialogResult.OK)
    {
        // Save Last selected folder.
        LastSelectedFolder = fbFolderBrowser.SelectedPath;
    }
}
-3

I know this is a very old thread, but none of the answers point to the simplest way to re-open the file browser on user's last location.

simply define RestoreDirectory = true. Check the example

var fd = new OpenFileDialog
   {
       Filter = @"All Files|*.*",
       RestoreDirectory = true,
       CheckFileExists = true
   };

Class OpenFileDialog api reference

https://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.restoredirectory(v=vs.110).aspx

Unless I misunderstood the intention of the post, this is by far the simplest way to achieve it. However, if you do need to print this last location, then check the other

4
  • Neat and simpe :) Aug 31, 2017 at 0:47
  • 1
    This question is about the FolderBrowserDialog, not about OpenFileDialog. Nov 15, 2017 at 14:36
  • @GeertBellekens, the question did not specified a particular class to be used. Here's the question: "I want to open the browse window to the last folder he accessed and save it. Next time he clicks on the button, it'll automatically select that folder." So I don't understand the downvote. Can you clarify? Besides, the solution is exactly the same using either FolderBrowserDialog or OpenFileDialog. Both have the same property which serves the purpose which is to open the dialog at the same location from previous usage. Instead of just downvoting, should give an actual answer...
    – n4nd0_o
    Dec 14, 2017 at 0:51
  • 1
    RestoreDirectory is at the FileDialog level, it's not in FolderBrowserDialog.
    – user276648
    Feb 18, 2020 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.