开发者

How are the 2 config files related?

I am looking at someone else's C# code. In the project there's an app.config file and a xxx.config file, and AppSettings are in both of the files. But I didn't see any ConfigureSource, or build event. So how could the code use ConfigurationManager.AppSettings["key"] to access sett开发者_如何转开发ings in both files?

Thanks.

Some more clues:

I checked the 2 config files' properties and found that their build action are both "Embedded Resource"

Answer Revealed!

Maybe I lack some context, this is a Visual Studio Unit Test project. The app.config and xxx.config are both embedded as resources, and during the test initialization phase, theses config files are extracted from the resource and written into the vstesthost.exe.Config, which is used by the VSTestHost.exe.

I just don't know who on earth could invent such a un-human solution to deal with configurations.


ConfigurationManager.AppSettings["key"] will get a value just from one file for sure (from app.config). BUT you could have 2 files with appSettings section IF app.config "redirects" to the second file:

<appSettings file="settings.config" />

and second file contains all keys.

Here you could read more


You can merge appSettings like this:

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings file="xxx.config">
    <add key="foo" value="bar"/>
  </appSettings>
</configuration>

xxx.config

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
  <add key="xxx" value="yyy"/>
</appSettings>

Note that you have to set the Copy to Output Directory property of xxx.config to Copy always or Copy if newer because xxx.config isn't copied to the output directory by default.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜