I need to read key values ββfrom user sections in app / web.config.
I went through
Reading a key from Web.Config using ConfigurationManager
and
How to get a list of custom configuration sections in a .config file using C #?
However, they do not indicate how to read the user section when we need to explicitly specify the path to the configuration file (in my case, the configuration file is not in it by default)
An example of my web.config file:
<?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>
in which I need to read key value pairs inside MyCustomTag.
When I try (configFilePath is the path to my configuration file): -
var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath }; var config = ConfigurationManager.OpenMappedExeConfiguration( configFileMap, ConfigurationUserLevel.None); ConfigurationSection section = config.GetSection(sectionName); return section[keyName].Value;
I get the error message "I cannot access the protected internal indexer 'this' here" in the [keyName] section
Karan source share