在 C# 中读取默认应用程序设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading default application settings in C#
提问by Ozgur Ozcitak
I have a number of application settings (in user scope) for my custom grid control. Most of them are color settings. I have a form where the user can customize these colors and I want to add a button for reverting to default color settings. How can I read the default settings?
我的自定义网格控件有许多应用程序设置(在用户范围内)。其中大部分是颜色设置。我有一个表单,用户可以在其中自定义这些颜色,并且我想添加一个按钮以恢复默认颜色设置。如何读取默认设置?
For example:
例如:
- I have a user setting named
CellBackgroundColor
inProperties.Settings
. - At design time I set the value of
CellBackgroundColor
toColor.White
using the IDE. - User sets
CellBackgroundColor
toColor.Black
in my program. - I save the settings with
Properties.Settings.Default.Save()
. - User clicks on the
Restore Default Colors
button.
- 我有一个名为
CellBackgroundColor
in的用户设置Properties.Settings
。 - 在设计时,我将 的值设置
CellBackgroundColor
为Color.White
使用 IDE。 - 用户在我的程序中设置
CellBackgroundColor
为Color.Black
。 - 我用
Properties.Settings.Default.Save()
. - 用户点击
Restore Default Colors
按钮。
Now, Properties.Settings.Default.CellBackgroundColor
returns Color.Black
. How do I go back to Color.White
?
现在,Properties.Settings.Default.CellBackgroundColor
返回Color.Black
。我怎么回去Color.White
?
采纳答案by aku
@ozgur,
@ozgur,
Settings.Default.Properties["property"].DefaultValue // initial value from config file
Example:
例子:
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"
回答by jfs
How do I go back to Color.White?
我如何回到 Color.White?
Two ways you can do:
你可以通过两种方式:
- Save a copy of the settings before the user changes it.
- Cache the user modified settings and save it to Properties.Settings before the application closes.
- 在用户更改设置之前保存设置的副本。
- 在应用程序关闭之前缓存用户修改的设置并将其保存到 Properties.Settings。
回答by Matt Warren
I've got round this problem by having 2 sets of settings. I use the one that Visual Studio adds by default for the current settings, i.e. Properties.Settings.Default
. But I also add another settings file to my project "Project -> Add New Item -> General -> Settings File" and store the actual default values in there, i.e. Properties.DefaultSettings.Default
.
我通过 2 组设置解决了这个问题。我使用 Visual Studio 默认为当前设置添加的一个,即Properties.Settings.Default
. 但我也在我的项目“项目 -> 添加新项目 -> 常规 -> 设置文件”中添加了另一个设置文件,并将实际的默认值存储在那里,即Properties.DefaultSettings.Default
.
I then make sure that I never write to the Properties.DefaultSettings.Default
settings, just read from it. Changing everything back to the default values is then just a case of setting the current values back to the default values.
然后我确保我从不写入Properties.DefaultSettings.Default
设置,只是从中读取。将所有内容更改回默认值只是将当前值设置回默认值的一种情况。
回答by Ohad Schneider
Reading "Windows 2.0 Forms Programming", I stumbled upon these 2 useful methods that may be of help in this context:
在阅读“Windows 2.0 Forms Programming”时,我偶然发现了这两种可能在这种情况下有所帮助的有用方法:
ApplicationSettingsBase.Reload
ApplicationSettingsBase.Reload
From MSDN:
来自 MSDN:
Reload contrasts with Reset in that the former will load the last set of saved application settings values, whereas the latter will load the saved default values.
重新加载与重置的对比在于,前者将加载最后一组已保存的应用程序设置值,而后者将加载已保存的默认值。
So the usage would be:
所以用法是:
Properties.Settings.Default.Reset()
Properties.Settings.Default.Reload()
回答by Domagoj Peharda
Properties.Settings.Default.Reset()
will reset all settings to their original value.
Properties.Settings.Default.Reset()
将所有设置重置为其原始值。
回答by expelledboy
Im not really sure this is necessary, there must be a neater way, otherwise hope someone finds this useful;
我不太确定这是必要的,必须有一种更简洁的方法,否则希望有人觉得这很有用;
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));
}
}
usage;
用法;
var setting = Settings.Default.Properties.GetDefault<double>("MySetting");
回答by ta.speot.is
I found that calling ApplicationSettingsBase.Reset
would have the effect of resetting the settings to their default values, but also saving them at the same time.
我发现调用ApplicationSettingsBase.Reset
会产生将设置重置为默认值的效果,但同时也会保存它们。
The behaviour I wanted was to reset them to default values but not to save them (so that if the user did not like the defaults, until they were saved they could revert them back).
我想要的行为是将它们重置为默认值但不保存它们(这样如果用户不喜欢默认值,在保存它们之前他们可以将它们还原)。
I wrote an extension method that was suitable for my purposes:
我写了一个适合我的目的的扩展方法:
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);
}
}
}
}