我有一个在多个项目中使用的配置文件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
在这里不起作用。我该如何覆盖mykey1
而不破坏mykey2
?在这种情况下使用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
转换吗? - Alex R.