I have added App.config file in my project. I have created two settings from Project > Properties > Settings panel -
我已经在我的项目中添加了App.config文件。我已经创建了两个设置从项目>属性>设置面板-
I have noticed that when I am adding a setting, I can define scope as User
or Application
. -
我注意到,在添加设置时,我可以将scope定义为用户或应用程序。- - - - - -
- User
- 用户
- Application
- 应用程序
If I define setting as User
it goes touserSettings
section,
if I define setting as Application
it goes to applicationSettings
section
如果我将设置定义为User,它会转到touserSettings部分,如果我将设置定义为Application,它会转到applicationSettings部分
App.config
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
-
但是,这些设置可以以相同的方式从.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?
用户和应用程序范围的区别是什么?在什么情况下应该在这两者之间进行选择?
2 个解决方案
#1
49
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
稍后,当您重构安全模块时,您可能会在部署时将值更改为v1.22,以确保实现了正确的安全性
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:
然后,当Michelle改变她喜欢的皮肤时,应用程序会记住她的设置。这些属性现在看起来可能如下:
ApplicationSkin string User HelloKittySkin
#2
63
Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions. User-scope settings, however, can be written at run time, just as you would change any property value. The new value persists for the duration of the application session. You can persist changes to user settings between application sessions by calling the Settings.Save method.
只能读取应用程序范围设置,并且只能在设计时或通过修改.exe进行更改。在应用程序会话之间的配置文件。但是,用户范围设置可以在运行时编写,就像您更改任何属性值一样。新值在应用程序会话期间持续存在。通过调用设置,可以在应用程序会话之间持久化对用户设置的更改。保存方法。
Source on msdn: Using Settings in C#
msdn的源代码:使用c#中的设置
User settings are generally of use for persisting user preferences (e.g. app notification preferences etc.). Application settings would generally for items such as API keys etc.
用户设置通常用于持久化用户首选项(例如app通知首选项等)。应用程序设置一般用于诸如API密钥等项目。
As noted by @kmote, when user settings are modified and persisted at run time (via settings.Save()
), they will be written to a folder within User Profile storage (typically C:\Users\Username\AppData\Local\AppName in Windows 7 and above). In order to determine the location of the file programmatically, please see this post.
@kmote注意到的,当用户设置修改和保存在运行时(通过settings.Save()),他们将在用户配置文件被写入到一个文件夹存储(通常是在Windows 7中C:\Users\Username\AppData\Local\AppName以上)。要以编程方式确定文件的位置,请参阅本文。
#1
49
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
稍后,当您重构安全模块时,您可能会在部署时将值更改为v1.22,以确保实现了正确的安全性
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:
然后,当Michelle改变她喜欢的皮肤时,应用程序会记住她的设置。这些属性现在看起来可能如下:
ApplicationSkin string User HelloKittySkin
#2
63
Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions. User-scope settings, however, can be written at run time, just as you would change any property value. The new value persists for the duration of the application session. You can persist changes to user settings between application sessions by calling the Settings.Save method.
只能读取应用程序范围设置,并且只能在设计时或通过修改.exe进行更改。在应用程序会话之间的配置文件。但是,用户范围设置可以在运行时编写,就像您更改任何属性值一样。新值在应用程序会话期间持续存在。通过调用设置,可以在应用程序会话之间持久化对用户设置的更改。保存方法。
Source on msdn: Using Settings in C#
msdn的源代码:使用c#中的设置
User settings are generally of use for persisting user preferences (e.g. app notification preferences etc.). Application settings would generally for items such as API keys etc.
用户设置通常用于持久化用户首选项(例如app通知首选项等)。应用程序设置一般用于诸如API密钥等项目。
As noted by @kmote, when user settings are modified and persisted at run time (via settings.Save()
), they will be written to a folder within User Profile storage (typically C:\Users\Username\AppData\Local\AppName in Windows 7 and above). In order to determine the location of the file programmatically, please see this post.
@kmote注意到的,当用户设置修改和保存在运行时(通过settings.Save()),他们将在用户配置文件被写入到一个文件夹存储(通常是在Windows 7中C:\Users\Username\AppData\Local\AppName以上)。要以编程方式确定文件的位置,请参阅本文。