[英]c# Properties.Settings.Default Doesn't work as expected
我一直在研究使用LogMeIn备份自动执行备份检查的程序(基于Windows窗体的程序)。 我现在需要一种存储用户设置的方法,以轻松保存信息。 我从未使用过某种“内置”的“应用程序/用户”设置-决定尝试一下,但遇到了问题。
我现在添加了四个设置:IncludeCriteria(Specialized.StringCollection)ExcludeCriteria(Specialized.StringCollection)ReportPath(字符串)ReportType(整数)
但是这种行为并没有达到预期的效果(如图)。 在程序中保存了一些值之后,我将使用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)-甚至listBoxes都填充了我输入的一堆废话-但我无法在任何地方找到这些值。
任何帮助,将不胜感激!
玩笑
编辑/添加后必须保存
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.