C# App.config:用户与应用范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13046907/
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
App.config: User vs Application Scope
提问by Parag Meshram
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 Useror Application. -
我注意到,当我添加设置时,我可以将范围定义为Useror Application。——
- User
- Application
- 用户
- 应用
If I define setting as Userit goes touserSettingssection,
if I define setting as Applicationit goes to applicationSettingssection
如果我User在进入userSettings部分
时定义设置,如果我Application在进入applicationSettings部分时定义设置
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 Userand Applicationscope and under what circumstances one should choose between these two?
User和Applicationscope之间有什么区别,在什么情况下应该在这两者之间进行选择?
采纳答案by mcalex
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, youmight 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 hersettings. The properties may now look like:
然后,当 Michelle 更改为她喜欢的皮肤时,应用程序会记住她的设置。属性现在可能如下所示:
ApplicationSkin string User HelloKittySkin
回答by CalC
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.config 文件来更改。但是,用户范围设置可以在运行时写入,就像更改任何属性值一样。新值在应用程序会话期间保持不变。您可以通过调用 Settings.Save 方法在应用程序会话之间保持对用户设置的更改。
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.
用户设置通常用于保留用户首选项(例如应用程序通知首选项等)。应用程序设置通常用于 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\AppNamein Windows 7 and above). In order to determine the location of the file programmatically, please see this post.
正如@kmote 所指出的,当用户设置在运行时(通过settings.Save())被修改和保留时,它们将被写入用户配置文件存储中的一个文件夹(通常是 C:\Users\ Username\AppData\Local\ AppName在 Windows 7 及更高版本中)。为了以编程方式确定文件的位置,请参阅此帖子。


