app.config 文件和 XYZ.settings 文件有什么区别? [英] What is the difference between app.config file and XYZ.settings file?

查看:19
本文介绍了app.config 文件和 XYZ.settings 文件有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上正处于 .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.

但我找到了两种方法来做到这一点.当我在 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?

推荐答案

更新:在 ASP.NET Core Land 中,不再通过以下任一方式管理配置 - 看到 Travis Illig 的这篇精彩文章,在 Microsoft.Extension.Configuration 上带有 az 和Microsoft.Extensions.Configuration.Binder 实际上是所有这些的超集

UPDATE: In ASP.NET Core Land, configuration is no longer managed via either of these - see this fantastic writeup from Travis Illig with the a-z on Microsoft.Extension.Configuration and Microsoft.Extensions.Configuration.Binder which are effectively a superset of all this

设置(来自 .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 的最佳理由通常是,您可以通过遵循属性的用法(并且每个集都是 XML 文件中的单独块)来清楚地识别谁在使用代码库中的哪个设置.Configuration.appSettings 在本质上更具全局性——它只是一包属性,您不知道哪个 DLL、子系统或类依赖于特定的设置条目.有关更多信息,请参阅 Steven Smith 的这篇博文.

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.

最后,如果您对设置管理的了解还不够多,那么您不会打败 这篇关于该主题的 Rick Strahl 帖子 用于完整性或大量想法和角度.

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.

这篇关于app.config 文件和 XYZ.settings 文件有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆