在C#中读取默认应用程序设置

30

我有一些应用程序设置(用户范围内)用于我的自定义网格控件,其中大部分是颜色设置。我有一个表单,用户可以在其中自定义这些颜色,并想添加一个按钮以返回默认的颜色设置。如何读取默认设置?

例如:

  1. 我有一个名为CellBackgroundColor的用户设置,在Properties.Settings中。
  2. 在设计时,我使用IDE将CellBackgroundColor的值设置为Color.White
  3. 用户在我的程序中将CellBackgroundColor设置为Color.Black
  4. 我使用Properties.Settings.Default.Save()保存设置。
  5. 用户单击“恢复默认颜色”按钮。

现在,Properties.Settings.Default.CellBackgroundColor返回Color.Black。如何回到Color.White

7个回答

42

@ozgur,

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

例子:

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"

1
这仅适用于标量。我有一个类型为System.Collections.Specialized.StringCollection的属性。使用上述代码将返回原始XML,需要手动选择并创建集合。 - Bob Denny
嗨@BobDenny,你能解释一下你是怎么做到的吗? - ChihHao
这仅在您的属性是一个 string 类型时有效。如果是其他类型,您将无法直接将其分配回 Settings 对象的相应属性。 - Matt Arnold

14

在阅读《Windows 2.0 Forms Programming》时,我发现了两个有用的方法,可能对此情境有所帮助:

ApplicationSettingsBase.Reload

ApplicationSettingsBase.Reset

根据MSDN文档:

Reload和Reset的区别在于前者将加载上次保存的应用程序设置值,而后者将加载保存的默认值。

因此使用方法如下:

Properties.Settings.Default.Reset()
Properties.Settings.Default.Reload()

我发现“Reload与Reset相比,前者将加载上次保存的应用程序设置值,而后者将加载保存的默认值。”并不总是正确的。例如,在.Save然后.Reset之后,.Reload不会加载由.Save保存的设置 - 它没有任何效果。 - ChrisJJ
1
如果您只想重置一个设置而不是所有设置,该怎么办? - Kyle Delaney
1
@KyleDelaney 看起来 aku 在上面的回答中允许了这个(https://dev59.com/-3VD5IYBdhLWcg3wNY1Z#49289)。 - Ohad Schneider

5

我不太确定这是否必要,可能有更好的方法,但希望对某些人有用;

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));
    }
}

使用方法:

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

3

Properties.Settings.Default.Reset()会将所有设置重置为它们的原始值。


2
我通过创建两组设置来解决这个问题。我使用 Visual Studio 默认添加的一组设置,即 Properties.Settings.Default,作为当前设置。但是我还向项目中添加了另一个设置文件:"项目 -> 添加新项 -> 通用 -> 设置文件" 并将实际的默认值存储在那里,即 Properties.DefaultSettings.Default
我会确保从Properties.DefaultSettings.Default中读取设置,而不写入任何值。将所有值恢复为默认值只需要将当前值重设为默认值即可。

我也喜欢这种方法,因为如果更改设置名称,则调用{Settings.Default.Properties ["Foo"] as string}可能会导致异常。此外,强制转换可能会失败。我认为在数据复制(用户默认值和应用程序默认值)和类型安全之间,复制是较小的问题。 - Ohad Schneider
顺便说一句,确保您从不写入默认设置很容易 - 将所有设置都应用于应用程序范围,而不是用户范围。 - Ohad Schneider

1
我发现调用ApplicationSettingsBase.Reset会将设置重置为默认值,但同时也保存它们。
我想要的行为是将它们重置为默认值,但不保存它们(这样如果用户不喜欢默认值,直到保存之前他们可以将其恢复)。
我编写了一个适合我的目的的扩展方法:
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);
            }
        }
    }
}

1
如何回到Color.White?
你可以有两种方法:
1. 在用户更改设置之前保存一份副本。 2. 缓存用户修改的设置并在应用程序关闭前保存到Properties.Settings中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接