代码之家  ›  专栏  ›  技术社区  ›  Ozgur Ozcitak Vikash Choudhary

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

  •  26
  • Ozgur Ozcitak Vikash Choudhary  · 技术社区  · 17 年前

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

    例如:

    1. 我有一个名为 CellBackgroundColor 在里面 Properties.Settings
    2. 在设计时,我设置 Color.White
    3. 用户集 细胞背景色 Color.Black 在我的节目里。
    4. Properties.Settings.Default.Save()
    5. Restore Default Colors

    现在 Properties.Settings.Default.CellBackgroundColor 返回 颜色,黑色 ?

    7 回复  |  直到 15 年前
        1
  •  42
  •   aku    17 年前

    @奥兹古尔,

    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"
    
        2
  •  14
  •   Ohad Schneider    16 年前

    ApplicationSettingsBase.Reload

    ApplicationSettingsBase.Reset

    从MSDN:

    在这方面,重新加载与重置形成对比 默认值。

    因此,用法是:

    Properties.Settings.Default.Reset()
    Properties.Settings.Default.Reload()
    
        3
  •  5
  •   expelledboy    14 年前

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

    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
  •  3
  •   Domagoj Peharda    16 年前

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

        5
  •  2
  •   Matt Warren    17 年前

    我有两套设置来解决这个问题。我使用VisualStudio默认为当前设置添加的设置,即。 Properties.Settings.Default Properties.DefaultSettings.Default .

    然后我要确保我从来没有写信给 Properties.DefaultSettings.Default

        6
  •  1
  •   jfs    17 年前

    我怎样才能回到颜色,白色?

    有两种方法可以做到:

    • 在用户更改设置之前保存设置的副本。
    • 缓存用户修改的设置,并在应用程序关闭前将其保存到Properties.settings。
        7
  •  1
  •   ta.speot.is    13 年前

    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);
                }
            }
        }
    }
    
    推荐文章