您的位置:首页 > 移动开发

CodeProject - 使用.NET配置文件appSettings元素的File属性

2004-12-01 22:36 706 查看
原作者: Scott Bradley原文连接:http://www.codeproject.com/dotnet/appsettings_fileattribute.asp

翻译:小新0574

Introduction

介绍

If you need to share configuration settings among multiple .NET assemblies, the practice of maintaining separate config files for each assembly can quickly become tedious. For instance, if you have multiple executables within a directory that all need a 'ConnectionString' entry in their config file, the traditional method in .NET would be for each executable to have its own config file. This can become a burden in an enterprise environment when you need to change the connection string, as you would be forced to change each individual config file. Fortunately, there is a better approach. This better approach involves using the
File
attribute of the .NET config file's
appSettings
element. This attribute should contain the relative path to a custom config file, which all other applications can share. The description from MSDN on the
appSettings
File
attribute follows:

如果你需要在多个.NET程序集之间共享配置设置,那么维护每一个程序集的配置文件会很快使你觉得很烦闷。比如说,如果你在一个文件夹里有多个可执行文件需要在他们的配置文件里有一个'ConnectionString'入口,.NET传统的方式是每一个可执行文件都有它们自己的配置文件。如果在一个企业环境就会变成一个负担,因为当你需要改变连接字符串时,你不得不被迫改变每一个配置文件。幸运的是,有一个更好的办法。这个办法包括使用.NET配置文件
appSettings
元素的
File
属性的。这个属性可以包含相对于用户配置文件的相对路径,所有的程序都可以共享这个用户配置文件。MSDN对
appSettings
File
特性属性的描述如下:

Specifies a relative path to an external file containing custom application configuration settings. The specified file contains the same kind of settings that are specified in the
<add>
,
<remove>
, and
<clear>
elements and uses the same key/value pair format as those elements. The path specified is relative to the main configuration file. For a Windows Forms application, this would be the binary folder (such as /bin/debug), not the location of the application configuration file. For Web Forms applications, the path is relative to the application root, where the web.config file is located.

为一个包含在程序配置设置的外部文件指定一个相对路径。被指定的文件包含指定在
<add>
,
<remove>
, 和
<clear>
元素里那样的设置,就像那些元素一样的格式使用的键/值 对(pair)。指定的路径与主配置文件有关。对Windows Forms程序而言,这可能是一个binary文件夹(比如说/bin/debug),而不是程序配置文件的位置。对Web Forms程序而言,路径是相对于根程序,那里有web.config文件。

Note that the runtime ignores the attribute if the specified file can not be found.

注意 [/b]如果找不到指定的文件,运行时将忽略特性。

Essentially, each executable's config file will contain an entry such as:

本质上来说,每一个可执行文件的配置文件包含如下所示的一个入口:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="settings.config">
</appSettings>
</configuration>
where "settings.config" is a custom config file which looks something like this:

其中"settings.config"是一个用户配置文件,内容如下所示:

<appSettings>
<add key="Setting1" value="This is Setting 1 from settings.config" />
<add key="Setting2" value="This is Setting 2 from settings.config" />
<add key="ConnectionString" value="ConnectString from settings.confg" />
</appSettings>
When you run your application, you simply use the
AppSettings
property from
System.Configuration.ConfigurationSettings
to reference the configuration setting,

当你运行你的程序,你可以从
System.Configuration.ConfigurationSettings
简单的使用
AppSettings
属性引用配置设置:

Dim s As String = _
System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
In this case, if there is a '
ConnectionString
' key in the individual config file, that value will be used. Otherwise, the value will be retrieved from the shared "settings.config" file.

在这种情况,如果有一个'
ConnectionString
'键在一个单独的配置文件里,那个值就会相应的使用。否则,这个值就会在共享的"settings.config"文件里取得。

As indicated above, if you choose, each individual config file can also define the keys which are present in the shared config file. In that case, the individual config file settings will take precedence over the shared config file. That could be useful in a situation where you want to quickly test a new setting without disrupting the other applications which are using the shared config file.

就像上面指出的,如果你想选择(另一种方案),每一个配置文件也可以定义那些存在于共享配置文件里的键。在那种情况,单独的配置文件设置将会优先于共享配置文件。在这样一种情况是很有用的:你想快速测试一个新的设置而不想中断在共享配置文件里使用的其它程序。

Hopefully, this article has shed some light on the sometimes confusing world of .NET configuration files. I hope many of you find this method useful when dealing with multiple configuration files. Good luck.

希望这配能够帮助您理清.NET配置文件里容易混淆的地方。也希望大家能在处理多配置文件的时候发现这个办法有用。祝好运!

翻译完这篇文章,对最后提到的优先级问题有一些疑惑,我自己做了一个小实验,步骤如下:

在VS.NET2003里新建一个WinForm程序

添加一个App.config项,内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="mysetting.config">
<add key="Setting1" value="my value"></add>
</appSettings>
</configuration> 在debug文件夹下新建文件mysetting.config,内容如下:

<appSettings>
<add key="Setting1" value="This is Setting 1 from mysetting.config" />
<add key="Setting2" value="This is Setting 2 from mysetting.config" />
<add key="ConnectionString" value="ConnectString from mysetting.confg" />
</appSettings>
在窗体上放置一个textBox控件和Button控件

在Button的Click事件里边写如下代码:

textBox1.Text = System.Configuration.ConfigurationSettings.AppSettings["Setting1"];

运行结果,textBox显示的是:This is Setting 1 from mysetting.config。

这是为什么呢?原文不是说共享配置文件优先级低吗?

还请知道的大虾指教!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: