C# App.config 与设置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13100498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# App.config vs Settings File
提问by IEnumerable
This may sound like a trivial question, however I have looked over the web briefly and what I found was that app.configis basically an older mechanism for storing Application key/pairs of data for the application.
这听起来像是一个微不足道的问题,但是我简要地浏览了网络,我发现它app.config基本上是一种用于存储应用程序密钥/应用程序数据对的旧机制。
What I want to know is there any reason we (as .NET developers) would opt to use app.configover a Settings file?
我想知道的是有什么原因,我们(作为.NET开发人员)将选择使用app.config过Settings file?
-Can someone please provide some pros and cons on both so we can use them properly.
-有人可以提供一些优点和缺点,以便我们可以正确使用它们。
thanks again
再次感谢
采纳答案by farfareast
App.config for desktop applications and Web.config for web applications are part of .NET configuration system. Primarily they are used to control .NET framework settings in respect to our application. These are such configuration settings as substitutions of versions of assemblies (section <assemblyBinding>), substitution of .NET framework version (<startup>) etc. (see msdnfor the full app.config schema.) One section is dedicated for custom settings of application developers (<appSettings>). There is also a possibility to create custom sections. So, when we need to store settings we can either piggy-back on the app.config or create our own separate configuration files.
用于桌面应用程序的 App.config 和用于 Web 应用程序的 Web.config 是 .NET 配置系统的一部分。它们主要用于控制与我们的应用程序相关的 .NET 框架设置。这些是诸如替换程序集版本(<assemblyBinding> 部分)、替换 .NET 框架版本(<startup>)等的配置设置(有关完整的 app.config 架构,请参阅msdn。)一节专门用于自定义设置应用程序开发人员 (<appSettings>)。还可以创建自定义部分。因此,当我们需要存储设置时,我们可以搭载 app.config 或创建我们自己的单独配置文件。
Here are pros and contras of using app.config:
以下是使用 app.config 的优缺点:
Pro: There is already a standard API in .NET to read settings from appSettings section. If you only need just a couple of config settings, it is much easier to use this ready API than to develop and test your own class to read your config files. Also, app.config file is already included in VS project for you.
Pro: There is a standard hierarchy of machine.config/app.config. If you plan such settings that can be set machine-wide and overridden or left as-is for individual applications, you should use app.config.
Pro/Con: App.config is cached in run-time. If you anticipate updates of it while your application is running, you need to specifically request refresh of certain section of config file. For web.config the web app is automatically restarted when something is changed in the file. This is quite convenient.
Con: app.config is stored in the same directory as your .exe file. Normally it will be in a subfolder of C:\Program Files. This directory is extra protected in Windows 7 from writing. You need to be member of Administrators group to write there and if your UAC (User Access Control) level in Control Panel is not set to 0 (which normally is not), you will be asked by the OS to confirm writing to c:\Program Files. So, users without Administrator rights will not be able to change configuration in app.config. Same goes for changing your settings programmatically: your application will get exception when attempts to write app.config if it runs not under an admin user on Windows 7. Your own config files usually go to C:\ProgramData\ or c:\Users subfolder (on Windows 7). These locations are friendlier to writing by users or programs.
Con: If user edited your app.config file and accidentally corrupted it, the whole application will not start with some obscure error message. If your separate config file is corrupted, you will be able to provide more detailed error message.
优点:.NET 中已经有一个标准 API 可以从 appSettings 部分读取设置。如果您只需要几个配置设置,那么使用这个现成的 API 比开发和测试您自己的类来读取您的配置文件要容易得多。此外,app.config 文件已经包含在 VS 项目中。
优点:machine.config/app.config 有一个标准的层次结构。如果您计划的此类设置可以在机器范围内设置并为单个应用程序覆盖或保持原样,则应使用 app.config。
优点/缺点:App.config 在运行时缓存。如果您希望在应用程序运行时更新它,则需要专门请求刷新配置文件的某些部分。对于 web.config,当文件中的某些内容发生更改时,Web 应用程序会自动重新启动。这相当方便。
缺点:app.config 与您的 .exe 文件存储在同一目录中。通常它位于 C:\Program Files 的子文件夹中。此目录在 Windows 7 中受到额外保护,无法写入。您需要成为管理员组的成员才能在那里写入,如果您在控制面板中的 UAC(用户访问控制)级别未设置为 0(通常不是),操作系统将要求您确认写入 c:\程序文件。因此,没有管理员权限的用户将无法更改 app.config 中的配置。以编程方式更改设置也是如此:如果您的应用程序不是在 Windows 7 上的管理员用户下运行,则在尝试编写 app.config 时会出现异常。您自己的配置文件通常位于 C:\ProgramData\ 或 c:\Users 子文件夹(在 Windows 7 上)。这些位置对用户或程序编写更友好。
缺点:如果用户编辑了您的 app.config 文件并意外损坏了它,整个应用程序将不会以一些模糊的错误消息启动。如果您的单独配置文件已损坏,您将能够提供更详细的错误消息。
In conclusion: app.config gives you easier (faster development) approach, mostly suitable for read-only settings. Custom settings file gives you more freedom (where to store file, validation/error handling, more flexibility with its schema) but requires more work during development.
总之: app.config 为您提供了更简单(更快的开发)的方法,最适合只读设置。自定义设置文件为您提供更多自由(文件存储位置、验证/错误处理、其架构的更大灵活性),但在开发过程中需要更多工作。
回答by Kevin
You have it backwards, the settings file (or ini file as they were originally called) was the mechanism used to hold application settings (key/value pairs) prior to Windows 95. With the release of Windows 95 it was recommended that application settings be moved into the Windows Registry (which proved problematic since if you screwed up your registry your Windows may no longer be able to start).
反过来说,在 Windows 95 之前,设置文件(或最初称为 ini 文件)是用于保存应用程序设置(键/值对)的机制。随着 Windows 95 的发布,建议应用程序设置为进入 Windows 注册表(事实证明这是有问题的,因为如果你搞砸了你的注册表,你的 Windows 可能无法再启动)。
The .config file came into play with .Net. The XML format allows more dynamic and complex settings configurations than simple key/value pairs.
.config 文件开始与 .Net 一起使用。XML 格式允许比简单的键/值对更动态和更复杂的设置配置。
The modern user/settings file is an XML extension of the .config file (settings that can override certain settings in the .config under specific conditions).
现代用户/设置文件是 .config 文件的 XML 扩展名(可以在特定条件下覆盖 .config 中的某些设置的设置)。

