Pergunta

I would like to have two .settings file. One for the default value of setting and the other one for the user choice. I would like that the two settings file contained the same setting Name but not necessarily the same value.

Maybe the solution does not need two .settings file but it is the only idea that I have.

In brief, I want this to be able to propose the user to choose to return to the default value after he has choose bad setting for his usage.

Edit :

I use visual studio 2010 and it is in a win Form project

All is just about how to restore default configuration when the setting.settings file has been changed by the user choices (how does I save these default config)

Foi útil?

Solução

The SettingsProperty object for each user setting (or any setting) can be obtained through the Properties collection on the Settings class. You can then look at the DefaultValue property to get a default value (the value before being set for a particular user. For example, if you'd normally access your property like this:

var value = Properties.Settings.Default.MySetting;

then you can get at the default value like this:

var defaultValue = Properties.Settings.Default.Properties["MySetting"].DefaultValue;

The default value is the one that you set in the settings editor in Visual studio.

Update:

I'm not aware of any other way of getting default values. To avoid the string property name, you could write an extension method to get the default value based on compiler-checked expression:

public static class SettingsExtensions
{
    public static T GetSettingDefaultValue<T, T2>(this T2 settings,
        Expression<Func<T2, T>> expression) where T2 : ApplicationSettingsBase
    {
        MemberExpression memberExpr = expression.Body as MemberExpression;
        return (T)settings.Properties[memberExpr.Member.Name].DefaultValue;
    }
}

And get the default value like this:

var defaultValue = Settings.Default.GetSettingDefaultValue(s => s.MySetting);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top