How to get values ​​from ConfigSection defined as NameValueSectionHandler when using ConfigurationManager.OpenMappedExeConfiguration

Retrieving values ​​from a configuration file that uses the section defined by System.Configuration.NameValueSectionHandler is easy when you use the current configuration file for the application.

Example configuration file.

<configuration> <configSections> <section name="MyParams" type="System.Configuration.NameValueSectionHandler" /> </configSections> <MyParams> <add key="FirstParam" value="One"/> <add key="SecondParam" value="Two"/> </MyParams> </configuration> 

Sample code that reads it easily.

 NameValueCollection myParamsCollection = ConfigurationManager.GetSection("MyParams") as NameValueCollection; 

This is code that does not work.

 NameValueCollection collection = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) .GetSection("MyParams") as NameValueCollection; 

The following compilation error failed.

It is not possible to convert the type "System.Configuration.ConfigurationSection" to "System.Collections.Specialized.NameValueCollection" through a reference conversion, box conversion, conversion for decompression, conversion conversion or null type conversion.

ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None) returns System.Configuration.Configuration, and Configuration.GetSection returns ConfigurationSection.

ConfigurationManager.GetSection returns an object.

So, how do I get my NameValueCollection back when I need to use OpenExeConfiguration?

+4
source share
1 answer

I came across my own answer two years ago.

NameValueSectionHandler - can I use this type of section to write to the application configuration file?

This is my approach to solving my current problem.

 ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = "path to config here" }; Configuration config = ConfigurationManager.OpenMappedExeConfiguration( configFileMap, ConfigurationUserLevel.None); ConfigurationSection myParamsSection = config.GetSection("MyParams"); string myParamsSectionRawXml = myParamsSection .SectionInformation.GetRawXml(); XmlDocument sectionXmlDoc = new XmlDocument(); sectionXmlDoc.Load(new StringReader(myParamsSectionRawXml )); NameValueSectionHandler handler = new NameValueSectionHandler(); NameValueCollection handlerCreatedCollection = handler.Create(null, null, sectionXmlDoc.DocumentElement) as NameValueCollection; Console.WriteLine(handlerCreatedCollection.Count); 

A method that works with any of the legacy types IConfigurationSectionHandler NameValueSectionHandler, DictionarySectionHandler, SingleTagSectionHandler.

 public static object GetConfigurationValues(string configFileName, string sectionName) { ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = configFileName }; Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None); ConfigurationSection section = config.GetSection(sectionName); string xml = section.SectionInformation.GetRawXml(); XmlDocument doc = new XmlDocument(); doc.Load(XmlReader.Create(new StringReader(xml))); string type = section.SectionInformation.Type; string assemblyName = typeof(IConfigurationSectionHandler).Assembly.GetName().FullName; ObjectHandle configSectionHandlerHandle = Activator.CreateInstance(assemblyName, section.SectionInformation.Type); if (configSectionHandlerHandle != null) { IConfigurationSectionHandler handler = configSectionHandlerHandle.Unwrap() as IConfigurationSectionHandler; return handler.Create(null, null, doc.DocumentElement); } return null; } 
+8
source

All Articles