【问题标题】:Reading default application settings in C#在 C# 中读取默认应用程序设置
【发布时间】:2008-09-08 07:30:47
【问题描述】:

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

例如:

  1. 我在Properties.Settings 中有一个名为CellBackgroundColor 的用户设置。
  2. 在设计时,我使用 IDE 将 CellBackgroundColor 的值设置为 Color.White。
  3. 用户在我的程序中将CellBackgroundColor 设置为Color.Black。
  4. 我用Properties.Settings.Default.Save()保存设置。
  5. 用户点击Restore Default Colors按钮。

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

【问题讨论】:

    标签: c# .net winforms application-settings


    【解决方案1】:

    @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"
    

    【讨论】:

    • 这仅适用于标量。我有一个 System.Collections.Specialized.StringCollection 类型的属性。使用上述方法会返回原始 XML,需要您选择并手动创建集合。
    • 嗨@BobDenny,你能解释一下你是怎么做的吗?
    【解决方案2】:

    在阅读“Windows 2.0 表单编程”时,我偶然发现了这两种有用的方法,在这种情况下可能会有所帮助:

    ApplicationSettingsBase.Reload

    ApplicationSettingsBase.Reset

    来自 MSDN:

    Reload 与 Reset 的对比在于 前者将加载最后一组 保存的应用程序设置值, 而后者将加载保存的 默认值。

    所以用法是:

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

    【讨论】:

    • 我发现“重新加载与重置相比,前者将加载最后一组保存的应用程序设置值,而后者将加载保存的默认值。”并不总是正确的。例如。在 .Save 然后 .Reset 之后,.Reload 不会加载 .Save 保存的集合 - 它没有效果。
    • 如果您只想重置一项设置而不是全部设置怎么办?
    • @KyleDelaney 看起来像上面 aku 的回答允许 (stackoverflow.com/a/49289/67824)。
    【解决方案3】:

    我不确定这是否必要,必须有一个更简洁的方法,否则希望有人觉得这很有用;

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        我通过两组设置解决了这个问题。我使用 Visual Studio 默认为当前设置添加的那个,即Properties.Settings.Default。但我还在我的项目“项目 -> 添加新项目 -> 常规 -> 设置文件”中添加了另一个设置文件,并将实际的默认值存储在其中,即Properties.DefaultSettings.Default。

        然后我确保我从不写入Properties.DefaultSettings.Default 设置,而只是从中读取。将所有内容更改回默认值只是将当前值设置回默认值的一种情况。

        【讨论】:

        • 我也更喜欢这种方法,因为如果更改设置名称,调用 {Settings.Default.Properties["Foo"] as string} 可能会导致异常。此外,铸造可能会失败。我认为在数据重复(用户默认和应用默认)和类型安全之间,重复是较小的邪恶
        • 顺便说一句,确保您从不写入默认设置很容易 - 将所有设置设置为应用程序范围,而不是用户范围
        【解决方案6】:

        如何返回 Color.White?

        两种方法:

        • 在用户更改之前保存设置的副本。
        • 缓存用户修改的设置,并在应用程序关闭前将其保存到 Properties.Settings。

        【讨论】:

          【解决方案7】:

          我发现调用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);
                      }
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2011-02-03
            • 1970-01-01
            • 1970-01-01
            • 2012-12-28
            • 1970-01-01
            • 2013-09-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多