【发布时间】:2011-09-24 20:56:43
【问题描述】:
我有一个用于多个项目的配置文件general.config,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="mykey1" value="myvalue1"/>
<add key="mykey2" value="myvalue2"/>
</appSettings>
在其中一个项目中,我需要覆盖两个设置之一。所以这个项目的app.config 看起来像:
<?xml version="1.0"?>
<configuration>
<appSettings file="general.config">
<remove key="mykey1"/>
<add key="mykey1" value="anothervalue"/>
<add key="mykey3" value="myvalue3"/>
</appSettings>
</configuration>
但是remove 在这里不起作用。如何在不破坏mykey2 的情况下覆盖mykey1? add 在这种情况下有效。我可以从ConfigurationManager 得到myvalue3。
编辑:general.config 在编译时自动复制到输出文件夹。不用担心路径问题。目前我得到了:
ConfigurationManager.AppSettings["mykey1"]
//I got "myvalue1", but I want "anothervalue" here
//that is, this item is "overrided", just like virtual methods in C#
ConfigurationManager.AppSettings["mykey2"]
//this setting will not be modified, currently it works fine
ConfigurationManager.AppSettings["mykey3"] //good
【问题讨论】:
-
你指的是
config转换吗? -
希望通过
file指定的路径是相对于配置的,否则会被忽略 -
@V4Vendetta:你说得对。编译时将general.config复制到输出文件夹。
标签: c# .net configuration app-config