我需要從app/web.config中的自定義節中讀取鍵值。在C#中讀取自定義配置節的鍵值
我通過
去Reading a key from the Web.Config using ConfigurationManager
和
How can I retrieve list of custom configuration sections in the .config file using C#?
然而,他們並不指定如何讀取自定義欄目時,我們需要明確指定路徑到配置文件(在我的情況下,配置文件不在它的默認位置)
我的web.config文件的示例:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<MyCustomTag>
<add key="key1" value="value1" />
<add key="key2" value="value2" />
</MyCustomTag>
<system.web>
<compilation related data />
</system.web>
</configuration>
其中我需要讀取MyCustomTag中的鍵值對。
當我嘗試(configFilePath是通向我的配置文件): -
var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
var config =
ConfigurationManager.OpenMappedExeConfiguration(
configFileMap, ConfigurationUserLevel.None);
ConfigurationSection section = config.GetSection(sectionName);
return section[keyName].Value;
我得到一個錯誤,說明在部分[的keyName]
請加入你的web.config(全部或部分)。 – 2013-05-13 14:35:01
與web.config中的自定義設置的路徑無關。你將使用'' –
Saravanan
2013-05-13 14:38:00
@FrancescoDeLisi Question edited。 – Karan 2013-05-14 05:01:49