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

查看:210
本文介绍了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.

但我发现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 只是让你检索字符串,强制您执行任何转换,并且没有默认的概念。 (配置类实际上已经被分流到一个组件以反映这一点 - 如果你想要显式地添加一个引用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.

ASIDE:还有 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天全站免登陆