c#Properties.Settings.Default无法按预期工作

时间:2010-05-16 02:22:46

标签: c# settings properties.settings

我一直致力于使用LogMeIn备份(基于Windows窗体的程序)自动执行备份检查的程序。我现在需要一种存储用户设置的方法,以便轻松保存信息。我从未使用过某种“内置”的应用程序/用户设置 - 并决定尝试它,但遇到了问题。

我现在添加了四个设置: 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) - 即使是listBox也填充了我放入的一堆废话 - 但我无法在任何地方找到这些值。

任何帮助将不胜感激!

约什

1 个答案:

答案 0 :(得分: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();
}
相关问题