简体   繁体   English

如何使用默认和用户配置的两个具有相同设置名称的setting.settings文件

[英]How to have two setting.settings file with the same setting name for default and user config

I would like to have two .settings file. 我想要两个.settings文件。 One for the default value of setting and the other one for the user choice. 一个用于设置的默认值,另一个用于用户选择。 I would like that the two settings file contained the same setting Name but not necessarily the same value. 我希望两个设置文件包含相同的设置名称,但不一定具有相同的值。

Maybe the solution does not need two .settings file but it is the only idea that I have. 也许解决方案不需要两个.settings文件,但这是我唯一的想法。

In brief, I want this to be able to propose the user to choose to return to the default value after he has choose bad setting for his usage. 简而言之,我希望这能够建议用户在选择了错误的使用设置后选择返回默认值。

Edit : 编辑:

I use visual studio 2010 and it is in a win Form project 我使用的是Visual Studio 2010,它在win表格项目中

All is just about how to restore default configuration when the setting.settings file has been changed by the user choices (how does I save these default config) 一切都是关于在用户选择更改settings.settings文件后如何恢复默认配置(如何保存这些默认配置)

The SettingsProperty object for each user setting (or any setting) can be obtained through the Properties collection on the Settings class. 可以通过Settings类上的Properties集合获得每个用户设置(或任何设置)的SettingsProperty对象。 You can then look at the DefaultValue property to get a default value (the value before being set for a particular user. For example, if you'd normally access your property like this: 然后,您可以查看DefaultValue属性以获取默认值(为特定用户设置此值之前的值。例如,如果您通常按以下方式访问属性:

var value = Properties.Settings.Default.MySetting;

then you can get at the default value like this: 那么您可以像这样获得默认值:

var defaultValue = Properties.Settings.Default.Properties["MySetting"].DefaultValue;

The default value is the one that you set in the settings editor in Visual studio. 默认值是您在Visual Studio的设置编辑器中设置的值。

Update: 更新:

I'm not aware of any other way of getting default values. 我不知道其他任何获取默认值的方法。 To avoid the string property name, you could write an extension method to get the default value based on compiler-checked expression: 为了避免使用字符串属性名,您可以编写一个扩展方法以基于编译器检查的表达式获取默认值:

public static class SettingsExtensions
{
    public static T GetSettingDefaultValue<T, T2>(this T2 settings,
        Expression<Func<T2, T>> expression) where T2 : ApplicationSettingsBase
    {
        MemberExpression memberExpr = expression.Body as MemberExpression;
        return (T)settings.Properties[memberExpr.Member.Name].DefaultValue;
    }
}

And get the default value like this: 并获得像这样的默认值:

var defaultValue = Settings.Default.GetSettingDefaultValue(s => s.MySetting);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM