AppSettings与applicationSettings(.NET app.config / Web.config)的优缺点 [英] Pros and cons of AppSettings vs applicationSettings (.NET app.config / Web.config)

查看:125
本文介绍了AppSettings与applicationSettings(.NET app.config / Web.config)的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发.NET Windows Forms应用程序时,我们可以在这些 App.config 标记之间进行选择,以存储我们的配置值。哪个更好?

When developing a .NET Windows Forms Application we have the choice between those App.config tags to store our configuration values. Which one is better?

<configuration>

  <!-- Choice 1 -->
  <appSettings>
    <add key="RequestTimeoutInMilliseconds" value="10000"/>
  </appSettings>

  <!-- Choice 2 -->
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5612342342" >
        <section name="Project1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5612342342" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <Project1.Properties.Settings>
      <setting name="TABLEA" serializeAs="String">
        <value>TABLEA</value>
      </setting>
    </Project1.Properties.Settings>
  </applicationSettings>

</configuration>


推荐答案

基本< appSettings> ; 更容易处理-只需将< add key = .... value = ... />

The basic <appSettings> is easier to deal with - just slap in a <add key="...." value="..." /> entry and you're done.

缺点是:没有类型检查,例如您不能安全地假设您要配置的号码确实存在一个号码-有人可以在该设置中输入一个字符串.....您只需以 ConfigurationManager [(key)] ,然后由您决定要处理的内容。

The downside is: there's no type-checking, e.g. you cannot safely assume your number that you wanted to configure there really is a number - someone could put a string into that setting..... you just access it as ConfigurationManager["(key)"] and then it's up to you to know what you're dealing with.

此外,随着时间的流逝,< appSettings> 可能会变得相当复杂和混乱,如果您的应用程序开始将内容放入其中(还记得旧的windows.ini文件吗?:-))。

Also, over time, the <appSettings> can get rather convoluted and messy, if lots of parts of your app start putting stuff in there (remember the old windows.ini file? :-)).

如果可以的话,我希望并建议使用您自己的配置部分-使用.NET 2.0,它真的变得非常容易,这样,您就可以:

If you can, I would prefer and recommend using your own configuration sections - with .NET 2.0, it's really become quite easy, That way, you can:


  • a)在代码中定义配置设置并具有它们是类型安全的
    并选中

  • b)您可以将 Your 设置与其他
    的设置区分开。而且您也可以重用您的配置代码!

  • a) Define your configuration settings in code and have them type-safe and checked
  • b) You can cleanly separate YOUR settings from everyone else's. And you can reuse your config code, too!

您可以通过一系列非常好的文章来揭开.NET 2.0配置系统的神秘面纱。在CodeProject上:

There's a series of really good articles on you to demystify the .NET 2.0 configuration system on CodeProject:


  1. 揭开.NET 2.0配置之谜

解码.NET 2.0配置的奥秘

破解.NET 2.0配置之谜

强烈推荐! Jon Rista出色地解释了.NET 2.0中的配置系统。

Highly recommended! Jon Rista did a great job explaining the configuration system in .NET 2.0.

这篇关于AppSettings与applicationSettings(.NET app.config / Web.config)的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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