Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8982549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:36:23+00:00 2026-06-15T20:36:23+00:00

Getting the values from a config file that uses a section defined by System.Configuration.NameValueSectionHandler

  • 0

Getting the values from a config file that uses a section defined by System.Configuration.NameValueSectionHandler is easy when you’re using the current config file for the application.

Example Config file.

<configuration>
  <configSections>
    <section name="MyParams" type="System.Configuration.NameValueSectionHandler" />
  </configSections>

  <MyParams>
    <add key="FirstParam" value="One"/>
    <add key="SecondParam" value="Two"/>
  </MyParams>
</configuration>

Example Code that easily reads it.

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

This is the code that doesn’t work.

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

That fails with the following error on compile.

Cannot convert type ‘System.Configuration.ConfigurationSection’ to ‘System.Collections.Specialized.NameValueCollection’ via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion.

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

ConfigurationManager.GetSection returns object.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:36:24+00:00Added an answer on June 15, 2026 at 8:36 pm

    I ran across my own answer from two years ago.

    NameValueSectionHandler – can i use this section type for writing back to the application config file?

    This is my approach to solve my current issue.

    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 IConfigurationSectionHandler types 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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Puppy meta data gets read in from config file using (General::Config) and creates this
I'm still searching solution for getting values from dynamic created object. Follwing code generates
In the below program,i am not getting values from printf . #include<stdio.h> int main()
I have a method for getting values from database. public virtual List<TEntity> GetValues( int?
Im getting 2 values from a table and I'd like to compare them. If
I am getting the values from database as an array <?php foreach($this->getlist as $value){
I have a doubt in listview android. I am getting 3 dynamic values from
I'm getting odd behavior (it generates only missing values) from the following loop -
So, I'm getting a bunch of string values from a JSON object and then
So I've been getting some mysterious uninitialized values message from valgrind and it's been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.