我有一個在多個項目中使用,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被複制到輸出文件夾。 –