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

I was working on a program to automate backup checks using LogMeIn backup (a window based program). Now I need a way to store user preferences so that information can be stored easily. I've never worked with app / user preferences that are somewhat "built in" and decided to give it a try, but ran into problems.

I added four settings: IncludeCriteria (Specialized.StringCollection) ExcludeCriteria (Specialized.StringCollection) ReportPath (string) ReportType (int)

But the behavior is not acting as expected (go figure). After saving some values โ€‹โ€‹in my program, I will go back to editing / viewing my settings values โ€‹โ€‹using the VS 2008 settings editor. None of my values โ€‹โ€‹are saved. While I think it might be because these values โ€‹โ€‹are only default values, wouldn't that be where they can be saved / read / changed?

Here is my upload 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 list. 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();



    }

      

It looks like when the form loads, the path I put in the first time seems to show up (ReportPath) - even the listBoxes fill up a bunch of crap that I put in, but I can't find those values โ€‹โ€‹anywhere.

Any help would be appreciated!

Josh

+2


a source to share


1 answer


You should save after editing / adding

Settings.Default.Save();

      



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

      

+4


a source







All Articles