Loading Individual Designer Default Values into Visual Studio .NET Settings

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.

// 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 SettingsPropertys 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.

// 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.

Related posts:

  1. ALT.NET for the Rest of Us
  2. .NET Console.Writeline Performance Issues
  3. A .NET Application that Never Dies

1 Response to “Loading Individual Designer Default Values into Visual Studio .NET Settings”


Leave a Reply

Subscribe

Categories