2

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?

2
  • The statement seems to be correct. Does it work with another key? Also, try to add explicit type casting to a (string). Commented May 8, 2013 at 16:29
  • Did try what you said it did not work , thank u Commented May 8, 2013 at 16:37

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 :
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.
add the keys to the started solutions app.config and it will work
0

You need to set the value before you can get it. Try

public static string ClientId
{
    get 
    { 
        return ConfigurationManager.AppSettings["IE32"]; 
    }
    set 
    { 
        ClientId = ConfigurationManager.AppSettings["IE32"];
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.