2012-08-27 54 views
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(); 
    } 
} 

每當用戶選擇一個文件夾,我想保存該路徑。然後,當他再次點擊瀏覽按鈕時,我想讓默認路徑成爲他的最後選擇。

以上不起作用。它只保存所選的最後一個路徑,並且只有在重新啓動應用程序時纔會返回。我將如何去保存在同一個應用程序會話中的最後一個路徑?

+0

您需要重新加載設置,一旦你保存它們。當然,我建議你有一個變量,當你加載程序時,應用程序正在運行引用時,將該設置加載進去。這樣可以讓您在應用程序關閉後將所述變量的當前值保存到設置中。 –

+1

什麼是AppVars?看起來你正在保存一個地方並從另一個地方讀取。 – Tergiver

回答

2

您需要重新加載設置:

Properties.Settings.Default.Reload(); 

注意,當沒有在調試模式(據我所知)運行這僅適用。

+0

我應該在哪裏添加這個?我把它放在Properties.Settings.Default.Save()後面。並運行可執行文件,仍然沒有運氣。 – Testifier

+0

我也在fbFolderBrowser.SelectedPath = AppVars.LastSelectedFolder之前添加了它;並沒有運氣。 :S – Testifier

+0

@Testifier將此行添加到btnBrowse_Click方法的開頭;不要忘記在發佈模式下試用。 – daryal

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

好吧,所以我最終創建了我自己的解決方案...它使用字典: – pcalkins

+0

我最終把「LoadConfig();」在主類構造函數中......所以就在「InitializeComponents();」之後。 – pcalkins

相關問題