C# App.config 与设置文件 [英] C# App.config vs Settings File

查看:37
本文介绍了C# App.config 与设置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能听起来像一个微不足道的问题,但是我简单地浏览了网络,我发现 app.config 基本上是一种用于存储应用程序密钥/数据对的旧机制申请.

This may sound like a trivial question, however I have looked over the web briefly and what I found was that app.config is basically an older mechanism for storing Application key/pairs of data for the application.

我想知道我们(作为 .NET 开发人员)有什么理由选择使用 app.config 而不是 Settings file 吗?

What I want to know is there any reason we (as .NET developers) would opt to use app.config over a Settings file ?

-有人可以提供一些优点和缺点,以便我们可以正确使用它们.

-Can someone please provide some pros and cons on both so we can use them properly.

再次感谢

推荐答案

用于桌面应用程序的 App.config 和用于 Web 应用程序的 Web.config 是 .NET 配置系统的一部分.它们主要用于控制与我们的应用程序相关的 .NET 框架设置.这些是诸如替换程序集版本( 部分)、替换 .NET 框架版本()等的配置设置(请参阅 msdn 用于完整的 app.config 架构.)一节专门用于应用程序开发人员的自定义设置().还可以创建自定义部分.因此,当我们需要存储设置时,我们可以搭载 app.config 或创建我们自己的单独配置文件.

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 msdn for 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 的利弊:

Here are pros and contras of using app.config:

  1. Pro:.NET 中已经有一个标准 API 可以从 appSettings 部分读取设置.如果您只需要几个配置设置,那么使用这个现成的 API 比开发和测试您自己的类来读取您的配置文件要容易得多.此外,app.config 文件已经包含在 VS 项目中.

  1. 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:machine.config/app.config 有一个标准的层次结构.如果您计划此类设置可以在机器范围内进行设置并为单个应用程序覆盖或保持原样,则应使用 app.config.

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.

优点/缺点:App.config 在运行时缓存.如果您希望在应用程序运行时更新它,则需要专门请求刷新配置文件的某些部分.对于 web.config,当文件中的某些内容发生更改时,Web 应用程序会自动重新启动.这相当方便.

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.

缺点:app.config 与您的 .exe 文件存储在同一目录中.通常它位于 C:\Program Files 的子文件夹中.此目录在 Windows 7 中受到额外保护,无法写入.您需要成为管理员组的成员才能在那里写入,如果您在控制面板中的 UAC(用户访问控制)级别未设置为 0(通常不是),操作系统将要求您确认写入 c:\程序文件.因此,没有管理员权限的用户将无法更改 app.config 中的配置.以编程方式更改设置也是如此:如果您的应用程序不是在 Windows 7 上的管理员用户下运行,则在尝试编写 app.config 时,您的应用程序将出现异常.您自己的配置文件通常转到 C:\ProgramData\ 或 c:\Users 子文件夹(在 Windows 7 上).这些位置更便于用户或程序编写.

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.

缺点:如果用户编辑了您的 app.config 文件并意外损坏了它,整个应用程序将不会以一些模糊的错误消息启动.如果您的单独配置文件已损坏,您将能够提供更详细的错误消息.

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.

总而言之: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.

这篇关于C# App.config 与设置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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