[ACCEPTED]-Reading default application settings in C#-application-settings

Accepted answer
Score: 43

@ozgur,

Settings.Default.Properties["property"].DefaultValue // initial value from config file

Example:

string foo = Settings.Default.Foo; // Foo = "Foo" by default
Settings.Default.Foo = "Boo";
Settings.Default.Save();
string modifiedValue = Settings.Default.Foo; // modifiedValue = "Boo"
string originalValue = Settings.Default.Properties["Foo"].DefaultValue as string; // originalValue = "Foo"

0

Score: 14

Reading "Windows 2.0 Forms Programming", I 7 stumbled upon these 2 useful methods that 6 may be of help in this context:

ApplicationSettingsBase.Reload

ApplicationSettingsBase.Reset

From MSDN:

Reload 5 contrasts with Reset in that the former 4 will load the last set of saved application 3 settings values, whereas the latter will 2 load the saved default values.

So the usage 1 would be:

Properties.Settings.Default.Reset()
Properties.Settings.Default.Reload()
Score: 5

Im not really sure this is necessary, there 2 must be a neater way, otherwise hope someone 1 finds this useful;

public static class SettingsPropertyCollectionExtensions
{
    public static T GetDefault<T>(this SettingsPropertyCollection me, string property)
    {
        string val_string = (string)Settings.Default.Properties[property].DefaultValue;

        return (T)Convert.ChangeType(val_string, typeof(T));
    }
}

usage;

var setting = Settings.Default.Properties.GetDefault<double>("MySetting");
Score: 3

Properties.Settings.Default.Reset() will reset all settings to their original 1 value.

Score: 2

I've got round this problem by having 2 10 sets of settings. I use the one that Visual 9 Studio adds by default for the current settings, i.e. Properties.Settings.Default. But 8 I also add another settings file to my project 7 "Project -> Add New Item -> General -> Settings 6 File" and store the actual default values 5 in there, i.e. Properties.DefaultSettings.Default.

I then make sure that I 4 never write to the Properties.DefaultSettings.Default settings, just read 3 from it. Changing everything back to the 2 default values is then just a case of setting 1 the current values back to the default values.

Score: 1

How do I go back to Color.White?

Two ways 1 you can do:

  • Save a copy of the settings before the user changes it.
  • Cache the user modified settings and save it to Properties.Settings before the application closes.
Score: 1

I found that calling ApplicationSettingsBase.Reset would have the effect 8 of resetting the settings to their default 7 values, but also saving them at the same 6 time.

The behaviour I wanted was to reset 5 them to default values but not to save them 4 (so that if the user did not like the defaults, until 3 they were saved they could revert them back).

I 2 wrote an extension method that was suitable 1 for my purposes:

using System;
using System.Configuration;

namespace YourApplication.Extensions
{
    public static class ExtensionsApplicationSettingsBase
    {
        public static void LoadDefaults(this ApplicationSettingsBase that)
        {
            foreach (SettingsProperty settingsProperty in that.Properties)
            {
                that[settingsProperty.Name] =
                    Convert.ChangeType(settingsProperty.DefaultValue,
                                       settingsProperty.PropertyType);
            }
        }
    }
}

More Related questions