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

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

问题描述

这听起来像一个简单的问题,但是我已经看过在网上简单地和我所发现的是,的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 通过设置文件

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.config文件的Web应用程序的.NET配置系统的一部分。它们主要用于在尊重我们的应用控制的.NET framework设置。这些是这样的配置设置组件的版本替换(章节< assemblyBinding>)的.NET framework版本替换(小于启动>)等(见的 MSDN 为全面的app.config模式)一节专门为应用程序开发者自定义设置(小于。的appSettings>)。还有一种可能性,以创建自定义部分。所以,当我们需要存储的设置,我们可以背驮式上的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. 临:有已经在.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.

临:有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.

的Pro /缺点: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从写保护。您需要成为Administrators组的成员写有,如果在控制面板中的UAC(用户访问控制)级别未设置为0(通常不是),你会被要求由操作系统来确认写入到c:\程序文件。因此,用户无需管理​​员权限就无法更改的app.config配置。也是一样的编程方式更改设置:您的申请将得到异常,如果它在一个管理员用户运行无法在Windows 7你自己的配置文件,通常去到C时,尝试写的app.config:\ ProgramData \或C:\用户的子文件夹(在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 VS设置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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