Avatar of CipherIS
CipherIS
Flag for United States of America asked on

C# ConfigurationManager.GetSection returns null

I am trying to get the section in my app.config file.  I'm using ConfigurationManager.GetSection but always get a null value.  Below is my code and app.config file.

Any ideas?

NameValueCollection nvc = (NameValueCollection)ConfigurationManager.GetSection("groupInfo/sectionInfo1");
if (nvc != null)
{
    for (int i = 0; i < nvc.Count; i++)
    {
        Console.WriteLine(nvc[i] + " = " + nvc.Keys[i]);
     }
}

Open in new window


<?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <configSections>
        <sectionGroup name="groupInfo">
            <section name="sectionInfo1" type="System.Configuration.NameValueSectionHandler"/>
            <section name="sectionInfo2" type="System.Configuration.NameValueSectionHandler"/>
        </sectionGroup>
    </configSections>

    <groupInfo>
        <sectionInfo1>
            <add key="myKeyA" value="AAA"/>
            <add key="myKeyB" value="BBB"/>
        </sectionInfo1>
    </groupInfo>
        
</configuration>

Open in new window

C#Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
CipherIS

8/22/2022 - Mon
kaufmed

I just pasted your code and config into a new project (verbatim), and it worked fine for me. Are you sure you put the config in the correct place?
CipherIS

ASKER
I created a DLL and added an app.config file.  Below is a snapshot of my proj
App.Config.jpg
ASKER CERTIFIED SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
CipherIS

ASKER
I did that after I sent you the pic.  So in MyConsole it works but MyDLL it doesn't.  So that means I have to create some properties on the DLL to pass the settings over to it.

So app.config will not work in DLL proj?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck