Click to See Complete Forum and Search --> : AppSettings returns null value


sunsilk10
July 8th, 2008, 09:56 AM
Hi, I have come across a rather strange problem, seems quite straightforward, but I can't figure out what is wrong here:


I have a C# program with an application configuration file (app.config added in Visual Studio 2005). The contents of app.config are:

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


Now when I try to read the values from my config file
string sTemplateFile = ConfigurationManager.AppSettings.Get("KeyName");

or

string sTemplateFile = ConfigurationManager.AppSettings["KeyName"];

it returns null value

I have created the config file using the VS template. I can see the config file in my debug folder also.

Can anyone shed some light what is going on?

Thanks

eclipsed4utoo
July 8th, 2008, 11:36 AM
what does this return when you run it?


bool hasKeys = ConfigurationManager.AppSettings.HasKeys();

sunsilk10
July 9th, 2008, 03:16 AM
hasKeys returns false.
I think I might know the cause of the problem.

I am creating a class, which will be added as a tool in another main application.

Now, when I debug my C# program. I am launching the main application which contains my tool. I guess it is looking for the app.Config in the main application rather than in my program.