Reading App.config to get the value based on key in c# library project

16,057

How many projects do you have?

I suspect you have 2 projects (or more) then app.config needs to be in the project that is being run not the project with the config class.

Also when you build your project if it is a console app or a windows app the bin directory should contain a .config file with the same name as your exe. In a web app it will be in the root of the app in a file called. web.config.

Share:
16,057
Anand S
Author by

Anand S

Updated on June 29, 2022

Comments

  • Anand S
    Anand S almost 2 years

    i am trying to read the setting from the app.config , it looks like below

        <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="Chrome" value="path to the chrome driver" />
        <add key="IE32" value="path to the IE32 driver" />
        <add key="IE64" value="path to the IE64 driver" />
        <add key="Url" value="url to the site"/>
      </appSettings>
    </configuration>
    

    i am using the following code to read the content

    using System;
    using System.Configuration;
    
    public static class Config
    {
        public static string ClientId
        {
            get 
            { 
                return ConfigurationManager.AppSettings["IE32"]; 
            }
        }
    }
    

    Why does it always return null?

  • Anand S
    Anand S almost 11 years
    Hi, thank u its a console app that and its in a solution with several other projects in it ,As you told i checked for the app.config its in the root directory instead of bin , can you tell why this happening thank u :
  • Dennis Christian
    Dennis Christian about 6 years
    Every solution gets a unique app.config. Multiple app.configs will not be merged on build or deploy. So your code correctly returns a null because the key is not present in the app.config of the started solution.
  • Dennis Christian
    Dennis Christian about 6 years
    add the keys to the started solutions app.config and it will work