App.config:用户与应用范围 [英] App.config: User vs Application Scope
问题描述
我在我的项目中添加了 App.config 文件.我从项目">属性">设置"面板创建了两个设置 -
我注意到,当我添加设置时,我可以将范围定义为 User
或 Application
.-
- 用户
- 申请
如果我将设置定义为 User
,它会转到 userSettings
部分,
如果我将设置定义为 Application
,它会转到 applicationSettings
部分
App.config
<预><代码><配置><用户设置><DemoApp.Properties.Settings><setting name="MySetting1" serializeAs="String"><value>Value1</value></设置></DemoApp.Properties.Settings></userSettings><应用程序设置><DemoApp.Properties.Settings><setting name="MySetting2" serializeAs="String"><value>Value2</value></设置></DemoApp.Properties.Settings></applicationSettings></配置>但是,可以从 .cs
-
代码
string mySetting1 = DemoApp.Properties.Settings.Default.MySetting1;字符串 mySetting2 = DemoApp.Properties.Settings.Default.MySetting2;
User
和 Application
作用域有什么区别,在什么情况下应该选择这两者?
基本上,应用程序设置不能在程序运行期间更改,而用户设置可以.然后应保存这些用户设置,以便用户在接下来运行应用程序时获得熟悉的体验.
例如,您可能使用不同的模块编写应用程序,并且需要确保主模块使用正确版本的安全模块.为此,您将设置一个应用程序范围设置,例如:
SecurityModuleVersion string Application v1.21
稍后重构安全模块时,您可能会在部署时将该值更改为 v1.22,以确保实施正确的安全性
另一方面,如果您的应用程序具有不同的外观",包括颜色更改、字体更改等,那么您可以设置用户设置,如下所示:
ApplicationSkin string User DefaultSkin
然后,当米歇尔改变她喜欢的皮肤时,应用程序会记住她的设置.属性现在可能如下所示:
ApplicationSkin string 用户 HelloKittySkin
I have added App.config file in my project. I have created two settings from Project > Properties > Settings panel -
I have noticed that when I am adding a setting, I can define scope as User
or Application
. -
- User
- Application
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>
But, these settings can be accessed in the same way from .cs
-
Code
string mySetting1 = DemoApp.Properties.Settings.Default.MySetting1;
string mySetting2 = DemoApp.Properties.Settings.Default.MySetting2;
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.
Edit: 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
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
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屋!