2010-05-16 90 views
2

我一直在努力使用LogMeIn備份(基於Windows窗體的程序)來自動執行備份檢查。我現在需要一種方法來存儲用戶設置,以便輕鬆保存信息。我從來沒有使用有點「內置」的應用程序/用戶設置 - 並決定嘗試它,但遇到了問題。c#Properties.Settings.Default不能按預期工作

我加了四個設置現在: IncludeCriteria(Specialized.StringCollection) ExcludeCriteria(Specialized.StringCollection) ReportPath(串) REPORTTYPE(INT)

但隨着預期的行爲不採取行動(去數字)。在我的程序中保存了一些值後,我使用VS 2008設置編輯器重新編輯/查看我的設置值。我的價值都沒有被存儲。雖然我認爲這可能是因爲這些值只是默認值,是不是可以存儲/讀取/更改的位置?

這裏是我的負荷形式的代碼(還是很不登大雅之堂):

private void setupForm() 
    { 
     txtPath.Text = BackupReport.Properties.Settings.Default.ReportPath == null ? "" : BackupReport.Properties.Settings.Default.ReportPath; 

     if (BackupReport.Properties.Settings.Default.ReportType == 0) 
     { 
      radioHTML.Checked = true; 
     } 
     else 
      radioExcel.Checked = true; 

     if (BackupReport.Properties.Settings.Default.IncludeCriteria.Count > 0) 
     { 
      listIncludeCriteria.DataSource = Properties.Settings.Default.IncludeCriteria; 


      //foreach (string s in Properties.Settings.Default.IncludeCriteria) 
      // listIncludeCriteria.Items.Add(s); 
     } 

     if (BackupReport.Properties.Settings.Default.ExcludeCriteria.Count > 0) 
     { 
      listExcludeCriteria.DataSource = BackupReport.Properties.Settings.Default.ExcludeCriteria; 

      //foreach (string s in Properties.Settings.Default.ExcludeCriteria) 
      // listExcludeCriteria.Items.Add(s); 
     } 





    } 

listIncludeCriteria只是一個列表框。當用戶保存我調用這個方法:

private void saveSettings() 
    { 
     //var settings = BackupReport.Properties.Settings; 
     if (txtPath.Text != "") 
     { 
      BackupReport.Properties.Settings.Default.ReportPath = txtPath.Text; 

     } 

     if (listIncludeCriteria.Items.Count > 0) 
     { 
      //BackupReport.Properties.Settings.Default.IncludeCriteria = (StringCollection)listIncludeCriteria.Items.AsQueryable(); 


      foreach (var i in listIncludeCriteria.Items) 
      { 
       if (!isIncludeDuplicate(i.ToString())) 
        BackupReport.Properties.Settings.Default.IncludeCriteria.Add(i.ToString()); 
      } 

     } 

     if (listExcludeCriteria.Items.Count > 0) 
     { 

      //BackupReport.Properties.Settings.Default.ExcludeCriteria = (StringCollection)listExcludeCriteria.Items.AsQueryable(); 

      foreach (var i in listExcludeCriteria.Items) 
      { 
       if (!isExcludeDuplicate(i.ToString())) 
        Properties.Settings.Default.ExcludeCriteria.Add(i.ToString()); 
      } 

     } 

     if (radioExcel.Checked == true) 
      BackupReport.Properties.Settings.Default.ReportType = 1; 
     else 
      BackupReport.Properties.Settings.Default.ReportType = 0; 


     BackupReport.Properties.Settings.Default.Save(); 
     //Properties.Settings.Default.Save(); 
     this.DialogResult = DialogResult.OK; 
     this.Close(); 



    } 

的奇怪的事情是加載窗體時,我把第一次的路徑似乎來了(ReportPath) - 甚至是列表框與一堆填充廢話我放入 - 但我無法在任何地方找到這些值。

任何幫助,將不勝感激!

喬希

+0

這個回答說,這一切http://stackoverflow.com/問題/ 1804302 /哪裏是數據爲屬性設置默認保存 – 2014-10-06 06:04:50

回答

4

你必須編輯後保存/添加

Settings.Default.Save(); 

一個簡單的例子,我用了很多

private void Main_Load(object sender, EventArgs e) 
{ 
    this.Location = Settings.Default.WindowLocation; 
} 

private void Main_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    Settings.Default.WindowLocation = this.Location; 
    Settings.Default.Save(); 
} 
+0

那麼,我確實有我的saveForm()方法(BackupReport.Properties.Settings.Default.Save()中的.Save() ;) - 但我知道我是什麼起訴是。我結束了使用BindingSource,綁定我的列表框到我的屬性列表,並得到它保存正確。謝謝! – Jack 2010-05-16 06:41:10

+1

@BrunoLM這不是答案。 – 2014-10-06 05:20:26

+0

@Jack你能解釋一下你是如何解決這個問題的?謝謝 – 2014-10-06 05:21:16