Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF application settings - resetting a single property

There is a way to reset application settings with Settings.Default.Reset()

Is there a way to reset only one property? Something like

Settings.Default.Properties["MyPropertyName"].Reset();
like image 612
nikita Avatar asked Aug 02 '12 16:08

nikita


2 Answers

You can use the Settings.Default.Properties["MyProperty"].DefaultValue to obtain the default value for the property, and set the property value to that.

like image 88
Smi Avatar answered Oct 26 '22 23:10

Smi


It's the PropertyValue that's need to be set in combinaison of Deserialized (the order matter) :

public void ResetOneSetting(string propertyName)
{
    SettingsPropertyValue propertyToReset = Settings.Default.PropertyValues.OfType<SettingsPropertyValue>().FirstOrDefault(p => p.Name == propertyName);
    if (propertyToReset != null)
    {
        propertyToReset.PropertyValue = propertyToReset.Property.DefaultValue;
        propertyToReset.Deserialized = false;
    }
}
like image 22
dalleria Avatar answered Oct 27 '22 00:10

dalleria