I added a key called MessageToLog to the appconfig file of a .net Framework4 Windows Application in C# like below:
<?xml version="1.0"?>
...
...
<appSettings>
...
...
<add key="MessageToLog" value="hello"/>
...
...
</appSettings>
How I am trying to reach they key in C# is:
string message2 = System.Configuration.ConfigurationManager.AppSettings["MessageToLog"];
Console.Writeln(message2);
However, I see null value instead of Hello at the output. What I am doing wrong? Where should I look for the mistake? Thanks
Make sure that your app.config is correctly named (It's called "app.config") and is in your console app project, not some other project.
A quick way to sanity check this - mess up your app.config and try to run the console app. Insert some characters like this:
<?xml version="1.0"?>xxx
^^^
Try to run the console app. It should fail with configuration errors. If it doesn't then the app.config you're editing isn't part of this project.
Also, did you add it using Add > New Item > General > Application Configuration File?
That makes sure that the correct elements are there. (Just wondering if you might have created it manually.) The file should minimally look like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MessageToLog" value="hello"/>
</appSettings>
</configuration>