App.config:用户与应用程序范围 [英] App.config: User vs Application Scope

查看:128
本文介绍了App.config:用户与应用程序范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在项目中添加了App.config文件。
我已经从项目>属性>设置面板中创建了两个设置-

I have added App.config file in my project. I have created two settings from Project > Properties > Settings panel -

我注意到添加设置时,可以将范围定义为 User Application 。 -

I have noticed that when I am adding a setting, I can define scope as User or Application. -


  1. 用户

  2. 应用程序

如果我将设置定义为 User ,则会转到 userSettings 部分,

,如果我将设置定义为 Application ,则会转到 applicationSettings 部分

If I define setting as User it goes touserSettings section,
if I define setting as Application it goes to applicationSettings section

App.config

<configuration>

    <userSettings>
        <DemoApp.Properties.Settings>
            <setting name="MySetting1" serializeAs="String">
                <value>Value1</value>
            </setting>
        </DemoApp.Properties.Settings>
    </userSettings>

    <applicationSettings>
        <DemoApp.Properties.Settings>
            <setting name="MySetting2" serializeAs="String">
                <value>Value2</value>
            </setting>
        </DemoApp.Properties.Settings>
    </applicationSettings>

</configuration>

但是,这些设置可以通过 .cs -

But, these settings can be accessed in the same way from .cs -

代码

string mySetting1 = DemoApp.Properties.Settings.Default.MySetting1;
string mySetting2 = DemoApp.Properties.Settings.Default.MySetting2;

User 应用程序的范围,在什么情况下应该在这两者之间进行选择?

What is the difference between User and Application scope and under what circumstances one should choose between these two?

推荐答案

基本上,在程序运行期间不能更改应用程序设置,而用户设置可以更改。然后应保存这些用户设置,以便在下次运行应用程序时为用户提供熟悉的体验。

Basically, application settings cannot be changed during the running of a program and user settings can. These user settings should then be saved so the user is presented with a familiar experience when (s)he runs the application next.

编辑:
例如,您可能使用不同的模块编写应用程序,并且需要确保您的主模块使用的是正确版本的安全模块。为此,您可以设置一个应用程序作用域设置,例如:

For examples, you might write your application with different modules, and need to ensure that your main module is using the correct version of your security module. For this you would set up an application-scope setting eg:

SecurityModuleVersion  string     Application      v1.21

有时在重构安全模块时,可能会在部署时将值更改为v1.22为了确保实现正确的安全性

Sometime later when you refactor the security module, you might change the value to v1.22 when you deploy to ensure the correct security is being implemented

另一方面,如果您的应用程序具有不同的皮肤,例如颜色,字体更改等,则可以设置用户设置以下内容:

On the other hand, if your application has different 'skins' with color changes, font changes etc, then you may setup a user setting something like the following:

ApplicationSkin        string     User              DefaultSkin

然后,当Michelle更改为她喜欢的皮肤时,应用程序会记住的设置。这些属性现在看起来像:

Then, when Michelle changes to the skin she prefers, the application remembers her settings. The properties may now look like:

ApplicationSkin        string     User              HelloKittySkin

这篇关于App.config:用户与应用程序范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆