Reading from app.config

I am trying to print Console.Writethe key value namefrom the following file app.config.

 <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="name" value="Chan" />
      </appSettings>
    </configuration>

C # code:

Console.Write(ConfigurationManager.AppSettings["name"]);

Nothing is printed in the console. Why is this?

Note. I added a link to the System.Configurationdll

+5
source share
2 answers

The code below gives you the contents of the active configuration file.

var content  = File.ReadAllLines(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

Check what you get like content, it contains key="name" value="Chan"or something else

if you specified <add key="name" value="Chan" />then ConfigurationManager.AppSettings ["name"] should be returned asChan

+4
source

, XML (app.config) , .
AppSettings. -

string sName = "";
sName = ConfigurationManager.AppSettings["name"].ToString();
-1

All Articles