views:

103

answers:

1

I thought the Reset() method repopulates the Settings with default values again, but it seems not to. How can I reload them with the default values?

  private void buttonLoadDefaultSettings_Click(object sender, EventArgs e)
  {
   FooSettings.Default.Reset();

   // Data grid will show an empty grid after call to reset.
   DataGridFoo.Rows.Clear();
   foreach (SettingsPropertyValue spv in FooSettings.Default.PropertyValues)
   {
    DataGridFoo.Rows.Add(spv.Name, spv.PropertyValue);
   }
  }

Update

private void buttonLoadDefaultSettings_Click(object sender, EventArgs e)
  {
   foreach (SettingsProperty sp in FooSettings.Default.Properties)
   {
    FooSettings.Default[sp.Name.ToString()] = sp.DefaultValue;
   }

   DataGridFoo.Rows.Clear();
   foreach (SettingsPropertyValue spv in FooSettings.Default.PropertyValues)
   {
    DataGridFoo.Rows.Add(spv.Name, spv.PropertyValue);
   }
  }

Removed the call to Reset() and set the property values manually to the default stored ones. I'm still eager to hear if this is the way it's supposed to be used or am I missing something?

A: 

I came across this thread because I encountered the same issue. I figured I'd report my findings for any future travelers who might come this way. I can't promise this is 100% accurate or complete 'cause I've been fiddling with it for an hour that's enough fiddling for one day even though I feel like there's still more to know. But at least they'll be some tips here. :)

Although the documentation for Reset() seems to indicate that saved settings are overwritten in the user.config file with the default values from the app.config file, this does not appear to be the case. It simply erases the settings from the user.config file, which, using the example above, results in FooSettings.Default.PropertyValues having a count of 0 because none exists after using Reset(). But there are ways to work with this result that don't necessitate repopulating the settings as the OP did. One way is to explicitly retrieve individual settings values like this:

// This always returns the value for TestSetting, first checking if an
// appropriate value exists in a user.config file, and if not, it uses 
// the default value in the app.config file.
FormsApp.Properties.Settings.Default.TestSetting;

Other ways involve using SettingsPropertyValueCollection and/or SettingsPropertyCollection:

// Each SettingsProperty in props has a corresponding DefaultValue property
// which returns (surprise!) the default value from the app.config file.
SettingsPropertyCollection props = FormsApp.Properties.Settings.Default.Properties;

// Each SettingsPropertyValue in propVals has a corresponding PropertyValue
// property which returns the value in the user.config file, if one exists.
SettingsPropertyValueCollection propVals = FormsApp.Properties.Settings.Default.PropertyValues;

So, returning to the original question, what you could have done is this:

private void buttonLoadDefaultSettings_Click(object sender, EventArgs e)
{
    FooSettings.Default.Reset();
    DataGridFoo.Rows.Clear();

    // Use the default values since we know that the user settings 
    // were just reset.
    foreach (SettingsProperty sp in FooSettings.Default.Properties)
    {
        DataGridFoo.Rows.Add(sp.Name, sp.DefaultValue);
    }
}
Nick