我一直在努力使用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) - 甚至是列表框與一堆填充廢話我放入 - 但我無法在任何地方找到這些值。
任何幫助,將不勝感激!
喬希
這個回答說,這一切http://stackoverflow.com/問題/ 1804302 /哪裏是數據爲屬性設置默認保存 – 2014-10-06 06:04:50