Question

I have this and this only saves the last folder used when the user closes the application and re-opens it.

private void btnBrowse_Click(object sender, EventArgs e)
{
     Properties.Settings.Default.Reload();

     fbFolderBrowser.SelectedPath = AppVars.LastSelectedFolder;

     if (fbFolderBrowser.ShowDialog() == DialogResult.OK)
     {
          Properties.Settings.Default.LastSelectedFolder = fbFolderBrowser.SelectedPath.ToString();
          Properties.Settings.Default.Save();
     }
}

Every time the user selects a folder, I want to save that path. Then, when he clicks the browse button again, I want the default path to be his last selection.

The above is not working. It only saves the last path selected and goes back to it only if I restart the app. How would I go about saving the last path in the same app session?

Was it helpful?

Solution

You need to reload the settings:

Properties.Settings.Default.Reload();

Note that this only works when not running in Debug mode (AFAIK).

OTHER TIPS

I'm gonna post my code here, because none of the answers I had seen addressed all the issues. This will save the location and re-load the settings for a file Browse dialog (file and folder browse dialogs are slightly different when getting path)... the answers above seem to be for the session only(?). Updated with new method to avoid config settings be lost with an update of clickonce app...

Code:

public class ConfigSettingsDictionary
    {
        public Dictionary<String, String> ConfigSettings = new Dictionary<String, String>();
    }
    ConfigSettingsDictionary MyConfigurationSettings = new ConfigSettingsDictionary();


private void SaveConfig()
    {
        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\YOUR_APP_NAME_PLUS_UNIQUE_VALUE.config"))
        {

            foreach (var pair in MyConfigurationSettings.ConfigSettings)
            {
                sw.WriteLine(pair.Key + "=" + pair.Value);
            }
        }

    }
    private void LoadConfig()
    {

        if (System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\YOUR_APP_NAME_PLUS_UNIQUE_VALUE.config"))
        {
            var settingdata = System.IO.File.ReadAllLines(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\YOUR_APP_NAME_PLUS_UNIQUE_VALUE.config");
            for (var i = 0; i < settingdata.Length; i++)
            {
                var setting = settingdata[i];
                var sidx = setting.IndexOf("=");
                if (sidx >= 0)
                {
                    var skey = setting.Substring(0, sidx);
                    var svalue = setting.Substring(sidx + 1);
                    if (!MyConfigurationSettings.ConfigSettings.ContainsKey(skey))
                    {
                        MyConfigurationSettings.ConfigSettings.Add(skey, svalue);
                    }
                }
            }
        }


    }
    private void UpdateConfig(Dictionary<String, String> keyvaluepairs)
    {

        foreach (var pair in keyvaluepairs)
        {
           if (!MyConfigurationSettings.ConfigSettings.ContainsKey(pair.Key))
           {
               MyConfigurationSettings.ConfigSettings.Add(pair.Key, pair.Value);
           }
           else
           {
               MyConfigurationSettings.ConfigSettings[pair.Key] = pair.Value;
           }
        }


    }

then I use it like this (this saves the folder chosen in a file browse dialog and also the file chosen):

  string lastused = "";

        if (MyConfigurationSettings.ConfigSettings.ContainsKey("openFileDialog2_last_used"))
        {
          MyConfigurationSettings.ConfigSettings.TryGetValue("openFileDialog2_last_used", out lastused);
        }


        if (lastused != "")
        {
            openFileDialog2.InitialDirectory = lastused;
        }
        else
        {
            openFileDialog2.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        }


        string filestring = "";
        if (template_filename == "")
        {
            try
            {
                if (openFileDialog2.ShowDialog() == DialogResult.OK)
                {


                    filestring = openFileDialog2.FileName;
                    String chosenPath = Path.GetDirectoryName(openFileDialog2.FileName);
                    Dictionary<String, String> settings_to_save = new Dictionary<String, String>();

                    settings_to_save.Add("openFileDialog2_last_used", chosenPath);
                    settings_to_save.Add("openFileDialog2_last_used_template_file", filestring);
                    UpdateConfig(settings_to_save);
}
         else return;

            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error.", "Invalid File", MessageBoxButtons.OK);
                return;
            }
private void YOURFORMNAME_Load(object sender, EventArgs e)
    {
// Load configuration file          
LoadConfig();
    }
 private void YOURFORMNAME_FormClosing(object sender, FormClosingEventArgs e)
    {
// Save configuration file
        SaveConfig();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top