问题描述
有人可以向我解释App.Config文件中AppSettings和ApplicationSettings部分之间的区别.为什么会有两个不同的部分显然做同样的事情?
Can someone please explain to me the difference between the AppSettings and ApplicationSettings sections in the App.Config file. Why are there two different sections that apparently do the same thing??
推荐答案
我相信app.config/web.config中的<appsettings/>
集合允许您将设置存储在键值对中,并且可以通过System.Configuration API,如下所示:
I believe that the <appsettings/>
collection in your app.config/web.config allows you to store settings in key-value pairs, and is accessed through the System.Configuration API, as follows:
string setting = System.Configuration.ConfigurationManager.AppSettings["settingName"];
设置只能作为字符串值存储和检索.
也可以通过System.Configuration.ConfigurationSettings访问它们,但是不建议使用这种方式.
Settings can only be stored and retrieved as string values.
They can also be accessed through System.Configuration.ConfigurationSettings, but this way has been deprecated.
配置文件中的<ApplicationSettings/>
集合以强类型方式存储您的设置,并且还允许您以强类型方式访问这些设置.在项目的 Properties 文件夹中的 settings.settings 文件中,VS会自动为您生成包装器类.要将设置文件添加到项目中,请右键单击您的项目,然后单击属性,然后打开设置标签.然后单击链接以添加新的设置文件. VS会自动为您生成一个.就这么简单.
The <ApplicationSettings/>
collection in your config file stores your settings in a strongly typed manner, and also allows you to access these settings in a strongly typed way. VS automatically generates wrapper classes for you, in the settings.settings file in the Properties folder of your project. To add a settings file to your project, right click on your project, and click Properties, then open the Settings tab. Then click the link to add a new settings file. VS will automatically generate one for you. It's that easy.
您通常按以下方式访问设置:
You usually access your settings as follows:
MyProjectName.Properties.Settings.Default.SettingName
请注意访问两个集合的方式不同.
Notice the difference in how the two collections are accessed.
第二种(不建议弃用的)存储设置的方法是更好的方法,它提供了很大的灵活性和功能.虽然需要一些学习-但这是值得的.
The second (non-deprecated) way of storing settings is the better way to do it, and provides lots of flexibility and power. It takes some learning though - but it is worth the effort.
这篇关于ApplicationSettings部分和AppSettings部分之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!