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

Jack picture Jack · May 16, 2010 · Viewed 10.9k times · Source

I've been working on a program to automate my backup checks with LogMeIn backup (a windows forms based program). 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)

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. 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. 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.

Any help would be appreciated!

Josh

Answer

BrunoLM picture BrunoLM · May 16, 2010

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