【问题标题】:How do I save the last folder selected by the user?如何保存用户选择的最后一个文件夹?
【发布时间】:2012-08-27 13:58:55
【问题描述】:

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

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

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

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

【问题讨论】:

  • 保存后需要重新加载设置。当然,我建议您在加载程序时以及在应用程序运行时引用该变量来加载设置。这将让您在应用程序关闭后将所述变量的当前值保存到设置中。
  • 什么是AppVars?您似乎正在保存一个地方并从另一个地方阅读。

标签: c# folderbrowserdialog


【解决方案1】:

您需要重新加载设置:

Properties.Settings.Default.Reload();

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

【讨论】:

  • 我应该在哪里添加这个?我把它放在 Properties.Settings.Default.Save(); 之后并运行可执行文件,但仍然没有运气。
  • 我还在 fbFolderBrowser.SelectedPath = AppVars.LastSelectedFolder;没有运气。 :S
  • @Testifier 将此行添加到 btnBrowse_Click 方法的开头;不要忘记在发布模式下尝试。
  • daryal,再看看我原来的帖子。我更新了它。仍然没有运气。我保存了项目,点击了构建然后转到可执行文件并运行它。它没有保存最后一个文件夹:/
  • 你是在发布模式下构建的吗?不在 DEBUG 模式下?
【解决方案2】:

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

【讨论】:

  • 好的,所以我最终在这里创建了自己的解决方案......它使用字典:
  • 我最终输入了“LoadConfig();”在主类构造函数中......所以就在“InitializeComponents();”之后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
  • 2023-01-10
相关资源
最近更新 更多