app.config 文件和 XYZ.settings 文件有什么区别?
我实际上正处于 .NET 相关内容的学习阶段,我正在探索如何保存应用程序。 我最终编写了自己的类,它将设置保存在 XML 文件中,然后我发现 .NET 本身支持保存应用程序设置。
但我找到了两种方法可以做到这一点。 当我在 Visual Studio 2008 中打开添加新项目对话框时,它提供了创建设置文件 (.settings) 或配置文件 (.config) 的选项。 两者有什么区别以及在什么场景下使用?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新:在 ASP.NET Core Land 中,配置不再通过以下任一方式进行管理 - 请参阅 Travis Illig 的精彩文章,其中包含
Microsoft.Extension.Configuration
和Microsoft.Extensions.Configuration.Binder
的 az,它们是实际上,所有这些设置的超集(均来自 .settings 集和
Configuration.AppSettings
),存储在 .config 文件中[与许多其他内容一起]。不同之处在于 .settings 内容[在 .NET 2.0 / VS2005 中添加]在一组属于在一起的设置之上分层了一个强类型类,而
Configuration.AppSettings
仅允许您检索字符串,强制您进行任何转换,并且没有默认值的概念。 (Configuration 类实际上已被分流到一个侧程序集中以反映这一点 - 如果需要,您需要显式添加对 System.Configuration 的引用)。如果您还没有 app.config,则将 .settings 添加到您的项目将导致添加一个 app.config 来容纳设置。 每次更改组件/应用程序的设置列表时,都会自动生成读取设置的类。
.Settings 的其他功能是能够将某些设置指定为特定于用户的设置(并且还可以通过一次调用保存特定于用户的设置)。
使用 .Settings 的最佳原因通常是,您可以通过跟踪属性的用法(每个集合都是 XML 文件中的单独块)来清楚地识别谁在代码库中使用哪个设置。
Configuration.appSettings
本质上更加全局化 - 它只是一组属性,您不知道哪个 DLL、子系统或类依赖于特定的设置条目。 有关更多信息,请参阅 Steven Smith 的这篇博文。最后,如果您对设置管理的了解还不够多,那么您将无法击败这篇关于该主题的 Rick Strahl 帖子 以获得完整性或大量的想法和角度。
旁白:还有 ASP.NET vNext 配置 内容,本文概述了,它非常灵活,并提供了配置设置管理的不同角度。
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
andMicrosoft.Extensions.Configuration.Binder
which are effectively a superset of all thisSettings (Both from a .settings set and
Configuration.AppSettings
), are stored in the .config file [alongside lots of other stuff].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).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.
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).
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.
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.
设置文件是一个资源文件,您可以在其中指定不同的设置及其默认值。
这些值本身是在应用程序配置文件(.config 文件)中配置的。
设置文件永远不会部署,因此您将需要配置文件来进行配置。
A settings file is a resource file in which you specify the different settings and their default value.
The values themselves are configured in the application configuration file (.config file).
A settings file is never deployed, so you will need the config file to do the configuration.
app.config 文件与应用程序存储在同一目录中。 普通用户没有写权限(例如在“程序文件”中)。
设置文件应存储在用户的“AppData”目录中(他具有读/写权限)。
因此,请使用用户可配置选项的设置文件。
The app.config file is stored in the same directory as the app. Normal users will not have write permissions (e.g. in "Program Files").
The settings file should be stored in the users "AppData" directory (where he has r/w permissions).
So use the settings file for user configurable options.