How to track the last folder selected by the user?

I thought using application settings would do the trick, but I won’t get 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 that he accessed and save it. The next time he presses the button, he automatically selects this folder.

I was thinking maybe I can use custom variables where I can change at runtime, but I can't get it to work. Can someone give me a hand?

+5
source share
5 answers

There are two places where you can find the last folder available to the user:

  • Recent Files and Folders: It can be found here: C:\Documents and Settings\USER\Recent
  • Registry: in the registry see here: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU

, :

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;
}

Windows XP, "" SaveFileDialog, , , (, Environment.CurrentDirectory).

, FileDialog , .

FileDialog.RestoreDirectory = true, FileDialog .

Windows Vista/Seven FileDialog.RestoreDirectory = true.

+2

, Project Designer , , . , .

    {
        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();
 }
+8

.

. thed.InitialPath

, :

, "" tab Name = Button1Path Type = String Scope =

:

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
            }
+1

, :

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;
    }
}
0

, , .

RestoreDirectory = true.

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

OpenFileDialog api reference

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

If I misunderstood the intention of this post, this is by far the easiest way to achieve it. However, if you need to print this last place, check out the others.

-1
source

All Articles