我有许多用于自定义网格控件的应用程序设置(在用户范围内)。其中大部分是颜色设置。我有一个表单,用户可以自定义这些颜色,我想添加一个按钮,以恢复到默认的颜色设置。如何读取默认设置?
例如:
Properties.Settings中有一个名为Properties.Settings的用户设置。CellBackgroundColor的值设置为Color.White。CellBackgroundColor设置为Color.Black。Properties.Settings.Default.Save()保存设置。Restore Default Colors按钮。现在,Properties.Settings.Default.CellBackgroundColor返回Color.Black。我怎么回Color.White?
发布于 2008-09-08 07:55:30
@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"发布于 2010-01-22 13:11:05
阅读"Windows 2.0窗体编程“时,我偶然发现了以下2种有用的方法,它们在这种情况下可能会有所帮助:
ApplicationSettingsBase.Reload
来自MSDN:
重新加载与Reset形成对比,前者将加载最后一组保存的应用程序设置值,而后者将加载保存的默认值。
所以它的用法是:
Properties.Settings.Default.Reset()
Properties.Settings.Default.Reload()发布于 2011-11-22 17:56:35
我真的不确定这是必要的,一定有一个更整洁的方式,否则希望有人发现这有用;
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");https://stackoverflow.com/questions/49269
复制相似问题