我有一些应用程序的设置(用户范围)为我定制格控制。他们大多数的色彩设置。我有一个表格的用户可以定义这些颜色和我想添加一个按钮,对于恢复以色默认设置。我怎么能读默认设置?

例如:

  1. 我有一个用户设定的命名 CellBackgroundColorProperties.Settings.
  2. 在设计时我设置的价值 CellBackgroundColorColor.White 使用的环境。
  3. 用户集 CellBackgroundColorColor.Black 在我的节目。
  4. 我保存的设置 Properties.Settings.Default.Save().
  5. 用户点击 Restore Default Colors 按钮。

现在, Properties.Settings.Default.CellBackgroundColor 返回 Color.Black.我怎么回去 Color.White?

有帮助吗?

解决方案

@奥兹古,

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"

其他提示

阅读"Windows2.0形式编程能力",我偶然发现了这2个有用的方法,可能有助于这方面:

ApplicationSettingsBase.重新加载

ApplicationSettingsBase.重置

从MSDN:

重新载入与重置在那 前者将负荷最后一组 保存应用程序设置的价值, 而后者将负荷的保存 默认的价值观。

这样的使用将是:

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

我不太确定这是必要的,必须有一个更简洁的方式,否则,希望有人认为这是有用的;

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

Properties.Settings.Default.Reset() 将重置的所有设置到他们的原始价值。

我已经得到了圆此问题通过具有2台的设置。我使用一些工作室增加了默认当前的设置,即 Properties.Settings.Default.但我还添加了另一个设置文件到我的项目"项目>添加的新项目事设置文件"和储存实际默认值,在那里,即 Properties.DefaultSettings.Default.

然后,我确定,我从来没有写到 Properties.DefaultSettings.Default 设置,只需阅读。改变一切回到默认值是那只是一个情况下设置的当前值回到缺省值。

我怎么回去以色。白色的?

两种方法可以做到:

  • 保存一个复制的设置之前用户的变化。
  • 高速缓存的用户修改后的设置和保存它的性质。设置之前的应用程序关闭。

我找到了,叫 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);
            }
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top