12

有一种方法可以重置应用程序设置Settings.Default.Reset()

有没有办法只重置一个属性?就像是

Settings.Default.Properties["MyPropertyName"].Reset();
4

6 回答 6

14

您可以使用Settings.Default.Properties["MyProperty"].DefaultValue获取属性的默认值,并将属性值设置为该值。

于 2012-08-02T16:19:20.127 回答
6

这是PropertyValue需要结合Deserialized(顺序问题)设置的:

public void ResetOneSetting(string propertyName)
{
    SettingsPropertyValue propertyToReset = Settings.Default.PropertyValues.OfType<SettingsPropertyValue>().FirstOrDefault(p => p.Name == propertyName);
    if (propertyToReset != null)
    {
        propertyToReset.PropertyValue = propertyToReset.Property.DefaultValue;
        propertyToReset.Deserialized = false;
    }
}
于 2016-09-01T13:27:59.420 回答
5

阅读.NET源代码时找到解决方案:

Settings.Default.PropertyValues["MyPropertyName"].SerializedValue = Settings.Default.Properties["MyPropertyName"].DefaultValue;
Settings.Default.PropertyValues["MyPropertyName"].Deserialized = false;
于 2012-08-03T13:46:17.610 回答
1

在我的情况下(.NET Framework 4.6.1,带有System.Drawing.Coloras 设置),我还必须重新分配 中的值Settings.Default,否则更改似乎被忽略了:

var propertyValue = Settings.Default.PropertyValues["myPropertyName"];

propertyValue.PropertyValue = propertyValue.Property.DefaultValue;
propertyValue.Deserialized = false;

Settings.Default["myPropertyName"] = propertyValue.PropertyValue;
于 2018-07-21T22:31:53.617 回答
0

我使用以下代码重置程序中的设置。它利用反射并允许您直接传递属性,而不是手动写出属性名称。它可以像ResetToDefaultValue(TestSettings.Default, x => x.TextFieldStuff);

这样做的好处是它并非所有设置都在其中,Settings但有些设置可能存储在不同的设置文件中。

//Way to use
//ResetToDefaultValue(TestSettings.Default, x => x.TextFieldStuff);
private static void ResetToDefaultValue<T1, T2>(T1 settings, Expression<Func<T1, T2>> property, bool saveOnReset = true)
{
    if (IsSameOrSubclass(typeof(ApplicationSettingsBase), settings.GetType()))
    {
        ApplicationSettingsBase s = settings as ApplicationSettingsBase;
        if (s != null)
        {
            MemberInfo member = GetMemberInfo(property);
            if (!s.PropertyValues[member.Name].UsingDefaultValue)
            {
                s.PropertyValues[member.Name].PropertyValue = s.PropertyValues[member.Name].Property.DefaultValue;
                s.PropertyValues[member.Name].Deserialized = false;
                s[member.Name] = s.PropertyValues[member.Name].PropertyValue; //Triggers the property changed
                if (saveOnReset)
                {
                    s.Save();
                }
            }
        }
    }
}

//Way to use
//GetMemberInfo((TestSettings testSettings) => testSettings.TextFieldStuff);
private static MemberInfo GetMemberInfo<T1, T2>(Expression<Func<T1, T2>> expression)
{
    if (IsSameOrSubclass(typeof(MemberExpression), expression.Body.GetType()))
    {
        MemberExpression member = (MemberExpression)expression.Body;
        return member.Member;
    }

    throw new ArgumentException(@"Expression is not a member access", nameof(expression));
}

private static bool IsSameOrSubclass(Type potentialBase, Type potentialDescendant)
{
    return potentialDescendant.IsSubclassOf(potentialBase)
           || potentialDescendant == potentialBase;
}

或者,如果您使用 C# 7.1 或更高版本,则可以使用特征匹配。

//Way to use
//ResetToDefaultValue(TestSettings.Default, x => x.TextFieldStuff);
private static void ResetToDefaultValue<T1, T2>(T1 settings, Expression<Func<T1, T2>> property, bool saveOnReset = true)
{
    //Requires C# >= 7.1
    if (settings is ApplicationSettingsBase s)
    {
        MemberInfo member = GetMemberInfo(property);
        if (!s.PropertyValues[member.Name].UsingDefaultValue)
        {
            s.PropertyValues[member.Name].PropertyValue = s.PropertyValues[member.Name].Property.DefaultValue;
            s.PropertyValues[member.Name].Deserialized = false;
            s[member.Name] = s.PropertyValues[member.Name].PropertyValue;
            if (saveOnReset)
            {
                s.Save();
            }
        }
    }
}

//Way to use
//GetMemberInfo((TestSettings testSettings) => testSettings.TextFieldStuff);
private static MemberInfo GetMemberInfo<T1, T2>(Expression<Func<T1, T2>> expression)
{
    //Requires C# >= 7.0
    if (expression.Body is MemberExpression member)
    {
        return member.Member;
    }

    throw new ArgumentException(@"Expression is not a member access", nameof(expression));
}
于 2019-01-19T13:11:28.230 回答
0

当我查看来自@dalleria 和@nikita 的答案时,我遇到了一个问题,即当我尝试获取 DefaultValue (SettingsProperty) 或 PropertyValue (SettingsPropertyValue) 或任何其他相同类型的深层副本时,PropertyValue (SettingsPropertyValue) 意外默认价值。

所以这是我对作为类的一部分的深拷贝函数的简短实现,所以我很抱歉没有将它重构为扩展或工具方法。

private SettingsPropertyValue settingsPropertyValue; // (ex. Settings.Default.PropertyValues["anyPropertyName"])

private SettingsProperty settingsProperty 
            => settingsPropertyValue.Property;

/// <summary>
/// Create a deep copy of <paramref name="value"/>.
/// </summary>
/// <param name="value">
/// Should be a deserialized value (ex. <see cref="SettingsPropertyValue.PropertyValue"/>) 
/// or a serialized value (ex. <see cref="SettingsProperty.DefaultValue"/>).
/// </param>
/// <param name="isDeserialized">Indicates whether <paramref name="value"/> is deserialized or serialized.</param>
private PropertyType copyValue<PropertyType>(object value, bool isDeserialized)
{
    var temporaryPropertyValue = settingsPropertyValue.PropertyValue;
    settingsPropertyValue.PropertyValue = value;

    if (isDeserialized)
        // We have to reassign, otherwise PropertyValue/SerializedValue of SettingsPropertyValue may be defaulted
        settingsPropertyValue.SerializedValue = settingsPropertyValue.SerializedValue; 

    settingsPropertyValue.Deserialized = false;
    var propertyValue = (PropertyType)settingsPropertyValue.PropertyValue;
    settingsPropertyValue.PropertyValue = temporaryPropertyValue;
    return propertyValue;
}

然后,您可以使用以下几行重置 ApplicationSettings/SettingsBase 实例的单个属性:

SettingsBase settings; // (ex. Settings.Default)

public void SetOriginalFromDefault()
{
    object defaultValue = settingsProperty.DefaultValue;
    defaultValue = copyValue(defaultValue, false);
    // We want to set the new value. It also triggers the INotifyPropertyChanged of settings, if the instance is from type ApplicationSettings
    settings[settingsProperty.Name] = defaultValue;
}
于 2019-09-23T10:37:20.397 回答