The VS.NET Settings
designer creates an ApplicationSettingsBase
Settings
class in Settings.Designer.cs (and optionally Settings.cs). The default values from the designer are saved in app.config and are loaded into the Settings.Default
singleton at runtime.
So, now you have a button on a properties page that says 'Reset to Factory Defaults' where you want to reload the designer default values back into your properties. If you want do this for all property values you can just use Settings.Default.Reset()
. But what if you only want to reset a subset of your properties?
There may be a better way to do this, but I couldn't find one. The following code does the job and will hopefully save someone from having to reinvent this wheel.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
// Resets properties to their designer default settings // public static void ResetToFactoryDefaults(SettingsPropertyCollection collection) { // Iterate through the settings and reset to their default values foreach (SettingsProperty setting in collection) { object prop = Properties.Settings.Default[setting.Name]; // The DefaultValue object is always a string but can be null. string defvalue = (string)(setting.DefaultValue ?? string.Empty); try { // Set the property with its converted default value Properties.Settings.Default[setting.Name] = TypeDescriptor.GetConverter(prop).ConvertFromString(defvalue); } catch (NotSupportedException ex) { // If the TypeConverter fails try to handle XML deserialization // ourselves (e.g. StringCollection). if (prop.GetType().IsSerializable) { StringReader stringReader = new StringReader(defvalue); XmlSerializer reader = new XmlSerializer(prop.GetType()); try { Properties.Settings.Default[setting.Name] = reader.Deserialize(stringReader); } catch (InvalidOperationException ex2) { Console.WriteLine( string.Format("ResetToFactoryDefaults deserialization failed: " + "setting={0} xml={1} {2}",setting.Name, defvalue, ex2.Message)); } } else { Console.WriteLine( string.Format("ResetToFactoryDefaults: setting={0} {1}", setting.Name, ex.Message)); } } } // Save the changes Properties.Settings.Default.Save(); } |
The ResetToFactoryDefaults
method takes a collection of SettingsProperty
s and uses the DefaultValue string to reset the value. Most value types (string, int, bool, etc.) worked with the TypeConverter
, but the StringCollection
class is not supported so the XML string has to be deserialized manually.
These helper methods show how just selected (and all) properties can be reset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Reset only the date and time format defaults. // public static void SetDateTimeFormatDefaults() { SettingsPropertyCollection col = new SettingsPropertyCollection(); col.Add(Properties.Settings.Default.Properties["DateFormat"]); col.Add(Properties.Settings.Default.Properties["TimeFormat"]); ResetToFactoryDefaults(col); } // Resets ALL properties to their designer default settings. // Same as Settings.Default.Reset() // public static void ResetToFactoryDefaults() { ResetToFactoryDefaults(Properties.Settings.Default.Properties); } |
This code was developed with VS 2005, but should also work in VS 2008.
Pingback: Recent URLs tagged Device - Urlrecorder