我需要从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