我有一个在多个项目中使用,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
@ V4Vendetta:你是对的。编译时,general.config被复制到输出文件夹。 –