Reading configuration key values ​​in C #

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

+4
source share
3 answers

Unfortunately, this is not as easy as it seems. The way to solve the problem is to get the file configuration file using the ConfigurationManager , and then work with the raw xml. Therefore, I usually use the following method:

 private NameValueCollection GetNameValueCollectionSection(string section, string filePath) { string file = filePath; System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument(); NameValueCollection nameValueColl = new NameValueCollection(); System.Configuration.ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = file; Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); string xml = config.GetSection(section).SectionInformation.GetRawXml(); xDoc.LoadXml(xml); System.Xml.XmlNode xList = xDoc.ChildNodes[0]; foreach (System.Xml.XmlNode xNodo in xList) { nameValueColl.Add(xNodo.Attributes[0].Value, xNodo.Attributes[1].Value); } return nameValueColl; } 

And the method call:

  var bla = GetNameValueCollectionSection("MyCustomTag", @".\XMLFile1.xml"); for (int i = 0; i < bla.Count; i++) { Console.WriteLine(bla[i] + " = " + bla.Keys[i]); } 

Result:

enter image description here

+7
source

I searched a lot to find a solution to the same problem. After some searching and debugging, I used this in my application. Hope this helps you.

https://synvistech.wordpress.com/2014/02/12/how-to-implement-custom-configsection-in-your-configuration/

0
source

Formo makes it very easy, for example:

 dynamic config = new Configuration("customSection"); var appBuildDate = config.ApplicationBuildDate<DateTime>(); 

See Form on configuration sections.

0
source

Source: https://habr.com/ru/post/1480502/


All Articles