20

I have the following code:

foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties)
{
    if (Double.TryParse(GenerateValue()), out result))
    {
        currentProperty.DefaultValue = result.ToString();

        Properties.Settings.Default.Save();
    }
}

It gets the new value from a mysql database. If I add a MessageBox.Show to show the new value it seems to be working fine but it doesn't actually save it. I assume this is because I am assigning the value to a variable...is there some way to do this?

Properties.Settings.Default.IndexOf(currentProperty.name).DefaultValue = result
Brandon
  • 1,735
  • 2
  • 22
  • 37

3 Answers3

34

This might work:

foreach (SettingsProperty  currentProperty in Properties.Settings.Default.Properties)
{
    Properties.Settings.Default[currentProperty.Name] = result.ToString();
    Properties.Settings.Default.Save();
}

Keep in mind that properties should have scope 'User' in order to be saved.

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
2

I would agree with your conclusion. What you are going have to do is get the property by the string value.

Properties.Settings.Default[string value] =

    foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties) 
    {    
    if (Double.TryParse(GenerateValue()), out result))  
       {        

Properties.Settings.Default[ currentProperty.Name ] = result.ToString();
          Properties.Settings.Default.Save(); 
        } 
    } 

The above is what you actually want.

Security Hound
  • 2,577
  • 3
  • 25
  • 42
0

I wanted to be retrieve the settings and save them back on program startup so that the entire settings file would be editable in appData. This is what I call when starting the program:

            foreach (SettingsProperty currentProperty in Settings.Default.Properties)
            {
                var value = Settings.Default[currentProperty.Name];
                Settings.Default[currentProperty.Name] = value;
                Settings.Default.Save();
            }

This seems to work regardless of the type of setting.

plcnut
  • 43
  • 5