How do I keep track of the last folder selected by a user?
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();
}
There are two places where you can find the last folder accessed by a user:
Recent Files and Folders
: It can be found here:C:\Documents and Settings\USER\Recent
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
.
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
}
}