Link to home
Create AccountLog in
Avatar of CipherIS
CipherISFlag 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

Avatar of kaufmed
kaufmed
Flag of United States of America image

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?
Avatar of 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
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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?