在开发.NET Windows窗体应用程序,我们有那些的App.config
标签之间的选择来存储的配置值。哪一个更好呢?
<结构>
<! - 选择1 - >
<的appSettings>
<添加键=RequestTimeoutInMilliseconds值=10000/>
< /的appSettings>
<! - 选择2 - >
< configSections>
< sectionGroup名=的applicationSettingsTYPE =System.Configuration.ApplicationSettingsGroup,系统,版本= 2.0.0.0,文化=中性公钥= b77a5c5612342342>
<节名称=Project1.Properties.SettingsTYPE =System.Configuration.ClientSettingsSection,系统,版本= 2.0.0.0,文化=中性公钥= b77a5c5612342342requirePermission =FALSE/>
< / sectionGroup>
< / configSections>
<的applicationSettings>
< Project1.Properties.Settings>
<设定名=TABLEAserializeAs =字符串>
<价值> TABLEA< /值GT;
< /设置>
< /Project1.Properties.Settings>
< /的applicationSettings>
< /结构>
解决方案
基本<的appSettings>
是容易应付过去 - 只拍了<添加键=...值=.../>
条目,你就大功告成了。
缺点是:有没有类型检查,例如:你不能安全地假设你想有配置真的是一个数字的号码 - 有人可以把一个字符串转换成该设置.....你刚刚接触它作为 ConfigurationManager中[(关键)]
然后就看你知道你正在处理的是什么。
此外,随着时间的推移,在<的appSettings>
可以得到相当令人费解和杂乱,如果很多你的应用程序的部分开始把东西在里面(记得旧窗.ini文件: - ?))
如果可以的话,我会preFER和推荐使用自己的配置节 - 用.NET 2.0,它真的变得非常容易,这样一来,您可以:
一)定义在code的配置设置,并让它们类型安全 并检查 b)你可以干净地从众人中分离的您的设置 别人的。你可以重复使用您的配置code,太!有一系列你真的好文章,对非神秘化$ C $的CProject的.NET 2.0的配置系统:
揭开.NET 2.0配置
解码.NET 2.0配置
开裂的.NET 2.0配置
强烈推荐!乔恩Rista做了伟大的工作,解释.NET 2.0。
配置系统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>
解决方案
The basic <appSettings>
is easier to deal with - just slap in a <add key="...." value="..." />
entry and you're done.
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.
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? :-)).
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) 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!There's a series of really good articles on you to demystify the .NET 2.0 configuration system on CodeProject:
Unraveling the mysteries of .NET 2.0 configuration
Decoding the mysteries of .NET 2.0 configuration
Cracking the mysteries of .NET 2.0 configuration
Highly recommended! Jon Rista did a great job explaining the configuration system in .NET 2.0.