简体   繁体   English

c#Properties.Settings.Default不能按预期工作

[英]c# Properties.Settings.Default Doesn't work as expected

I've been working on a program to automate my backup checks with LogMeIn backup (a windows forms based program). 我一直在研究使用LogMeIn备份自动执行备份检查的程序(基于Windows窗体的程序)。 I now need a way to store user settings, to save information easily. 我现在需要一种存储用户设置的方法,以轻松保存信息。 I've never worked with the Application/User settings that is somewhat "built-in" - and decided to try it, but ran into problems. 我从未使用过某种“内置”的“应用程序/用户”设置-决定尝试一下,但遇到了问题。

I added four settings for now: IncludeCriteria (Specialized.StringCollection) ExcludeCriteria (Specialized.StringCollection) ReportPath (string) ReportType (int) 我现在添加了四个设置:IncludeCriteria(Specialized.StringCollection)ExcludeCriteria(Specialized.StringCollection)ReportPath(字符串)ReportType(整数)

But the behavior doesn't act as expected (go figure). 但是这种行为并没有达到预期的效果(如图)。 After saving some values in my program, I go back into edit/view my settings values using the VS 2008 settings editor. 在程序中保存了一些值之后,我将使用VS 2008设置编辑器返回到编辑/查看设置值的状态。 None of my values are stored. 我的值均未存储。 While I think this may be because those values are just default values, wouldn't that be where they can be stored/read/changed? 虽然我认为这可能是因为这些值仅是默认值,但是这不是可以存储/读取/更改它们的地方吗?

Here is my load form code (still very unrefined): 这是我的加载表单代码(仍然非常不完善):

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 is just a listbox. listIncludeCriteria只是一个列表框。 When the user saves I call this method: 当用户保存时,我调用此方法:

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();



    }

The wierd thing is when the form loads, the path I put in the first time seems to come up (ReportPath) - even the listBoxes are populated with a bunch of crap I put in - yet I cant find these values anywhere. 奇怪的是,当表单加载时,我第一次输入的路径似乎出现了(ReportPath)-甚至listBoxes都填充了我输入的一堆废话-但我无法在任何地方找到这些值。

Any help would be appreciated! 任何帮助,将不胜感激!

Josh 玩笑

You have to save after editing/adding 编辑/添加后必须保存

Settings.Default.Save();

A simple example I use a lot 我经常使用的简单示例

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM