BudBrot
BudBrot

Reputation: 1411

Change the default value of a settingsvalue

id didnt find anything on Stackoverflow or on the internet, im sorry if this question was already posted (or if its simply impossible).

Is there a way to change the default value of a *.settigs-property, for example if i have the setting "USER"

[global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("TESTUSER")]
        public string SQLServer {
            get {
                return ((string)(this["USER"]));
            }
            set {
                this["USER"] = value;
            }
        }

would there be a way to change (at runtime) the DefaultSettingValueAttribute ("TESTUSER") to another value (i.e: "John Doe").

Thanks in advance

Upvotes: 1

Views: 2965

Answers (1)

Alex Filipovici
Alex Filipovici

Reputation: 32541

You can override the Properties property of the ApplicationSettingsBase class:

public override SettingsPropertyCollection Properties
{
    get
    {
        var properties = base.Properties;
        properties["SQLServer"].DefaultValue = 
            String.Format("John Doe {0}", DateTime.Now);
        return properties;
    }
}

Upvotes: 3

Related Questions