Illep
Illep

Reputation: 16849

Reading from a app.config file

I am trying to print to Console.Write the value of the key name from the following app.config file.

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

C# code :

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

Nothing gets printed in the console. Why is this ?

Note: I have added a reference to the System.Configuration dll

Upvotes: 5

Views: 22052

Answers (2)

Damith
Damith

Reputation: 63105

below code gives you the content of active config file.

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

Check what you get as content, is it contain key="name" value="Chan" or something else ?

if you given <add key="name" value="Chan" /> then ConfigurationManager.AppSettings["name"] should return as Chan

Upvotes: 4

vpv
vpv

Reputation: 938

Given that your XML file (app.config) is properly formatted, try below.
Declare a variable and assign the variable the AppSettings value. Similar like-

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

Upvotes: -1

Related Questions