2

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

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

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

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

4

2 回答 2

2

您需要重新加载设置:

Properties.Settings.Default.Reload();

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

于 2012-08-27T14:02:54.123 回答
0

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

代码:

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();
    }
于 2015-04-03T01:34:22.000 回答