如何保存用户选择的最后一个文件夹?

时间:2012-08-27 13:58:56

标签: c# folderbrowserdialog

我有这个,这只保存用户关闭应用程序并重新打开它时使用的最后一个文件夹。

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

每次用户选择文件夹时,我都想保存该路径。然后,当他再次单击浏览按钮时,我希望默认路径是他的最后一个选择。

以上不起作用。它只保存选定的最后一个路径,并且仅在我重新启动应用程序时才返回到它。我如何在同一个应用程序会话中保存最后一条路径?

2 个答案:

答案 0 :(得分:2)

您需要重新加载设置:

Properties.Settings.Default.Reload();

请注意,这仅在未在调试模式(AFAIK)中运行时才有效。

答案 1 :(得分:0)

我要在这里发布我的代码,因为我见过的所有答案都没有解决所有问题。这将保存位置并重新加载文件浏览对话框的设置(文件和文件夹浏览对话框在获取路径时略有不同)...上面的答案似乎仅适用于会话(?)。使用新方法更新以避免使用clickonce app更新配置设置...

代码:

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


    }

然后我像这样使用它(这会保存在文件浏览对话框中选择的文件夹以及选择的文件):

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