app.config文件和XYZ.settings文件之间的区别是什么?文件、区别、config、app

2023-09-02 11:45:42 作者:活着就是为了折腾

我其实在.NET相关的东西的学习阶段,我是研究如何保存应用程序。最后我写我自己的类,它保存了设置在一个XML文件,然后我发现,.NET本身支持保存应用程序设置。

I am actually in the learning phase of .NET related stuff and I was exploring how to save the application. I ended up writing my own class which saves the settings in an XML file and then I found that .NET itself supports saving application settings.

但我发现2种方法来做到这一点。当我打开Visual Studio 2008中添加新项对话框,它提供了选项来创建设置文件(.settings)或配置文件(.config文件)。请告诉我两者之间在什么情况下应该使用它们有什么区别?

But I found 2 ways to do that. When I open add new item dialog in Visual Studio 2008, it gives option to create Settings file (.settings) or a configuration file (.config). Whats the difference between both and in what scenario they should be used?

推荐答案

设置(无论是从.settings集和 Configuration.AppSettings ),都存储在.config文件[旁边很多其他的东西。

Settings (Both from a .settings set and Configuration.AppSettings), are stored in the .config file [alongside lots of other stuff].

所不同的是,.settings东西[其中加入在.NET 2.0 / VS2005]层的强类型类的一组属于一个整体设置的顶部,而 Configuration.AppSettings 只是让你检索字符串,强迫你做任何转换,并且不具有缺省值的概念。 (Configuration类实际上已经被分流到边组装,以反映这一点 - 你需要添加一个引用System.Configuration明确,如果你想的话)

The difference is that the .settings stuff [which was added in .NET 2.0 / VS2005] layers a strongly typed class on top of a set of settings that belong together whereas Configuration.AppSettings just lets you retrieve strings, forcing you to do any conversions, and doesnt have the notion of defaults. (the Configuration class has actually been shunted into a side assembly to reflect this - you need to add a reference to System.Configuration explicitly if you want it).

添加.settings到您的项目将导致一个app.config被添加到房子的设置,如果你不已经有一个。其内容设置的类每次更改设置列表中为您的组件/应用程序时自动生成的。

Adding a .settings to your project will result in an app.config being added to house the settings if you dont already have one. The class which reads the settings is automatically generated each time you change the list of settings for your component/application.

.Settings的其它特征是指定一些设置用户特定(和还保存用户特定的设置与单个呼叫)的能力。

Other features of .Settings is the ability to designate some settings as user-specific (and also to save the user-specific settings with a single call).

所有的最好的理由使用.Settings一般是你获得谁在使用明确识别能力通过下列属性的用法在code基地设置(每一组是在XML中一个单独的块文件)。 Configuration.appSettings 更是全球在它的本质 - 它只是一包的属性,你不知道哪个DLL,子系统或类依赖于一个特定的设置项。请参见从史蒂芬·史密斯的这篇博客文章的更多。

The best reason of all to use .Settings is generally that you gain the ability to clearly identify who is using which setting in a code base by following usages of properties (and each set is a separate block in the XML file). Configuration.appSettings is more global in it's nature - it's just a bag of properties and you dont know which DLL, subsystem or class depends on a particular setting entry. See this blog post from Steven Smith for much more.

最后,如果你还没有读够了有关设置的管理,你不会打这个里克施特拉尔后关于这个问题的的想法和角度完整性或纯粹的数量。

Finally, if you still haven't read enough about settings management, you're not going to beat this Rick Strahl post on the subject for completeness or sheer quantities of ideas and angles.

另外:另外还有 ASP.NET vNext配置的东西,的在这篇文章这是相当灵活,有不同的角度对配置设置概述管理。

ASIDE: There's also the ASP.NET vNext Configuration stuff, outlined in this article which is quite flexible and offers a different angle on configuration settings management.