ConfigurationSettings.AppSettings["mySetting"] returning null

D

Derrick

I have a C# console app, MyApp, and am placing an application config file in
the same debug dir as MyApp.exe. I have named the file MyApp.config, and
MyApp.exe.config. I cannot read the <appSettings> entries in the file
though, I get null back for properties.

config file
<configuration>
<appSettings>
<add key="mySetting" value="hello_world" />
....

Code is:
string test = ConfigurationSettings.AppSettings["hello_world"];

Any ideas? test winds up null. I'm going off...

http://www.codeguru.com/Csharp/Csharp/cs_network/configurationfilesinis/print.php/c7987/

Thanks in advance!

Derrick
 
C

Chris, Master of All Things Insignificant

Was it just a typo or shouldn't it be ["mySetting"] not ["hello_world"]

Chris
 
J

Jon Sagara

How did you add the file to your project? Try doing the following:

Right-click on the project and follow the Add popup menu
Click on Add New Item
Under the Local Project Items node, select Utility
Select Application Configuration File and click OK. Leave the file's name
as App.config.

AFAIK, those are the steps you must take to add a config file. I believe
that if you manually add it, it will get deleted every time you build the
project.

- Jon
 
D

Derrick

sorry, just a typo. I am using same code that I use in an ASP.NET web that
does work. only difference is that in the web the config is called
Web.Config

Chris said:
Was it just a typo or shouldn't it be ["mySetting"] not ["hello_world"]

Chris


Derrick said:
I have a C# console app, MyApp, and am placing an application config file
in
the same debug dir as MyApp.exe. I have named the file MyApp.config, and
MyApp.exe.config. I cannot read the <appSettings> entries in the file
though, I get null back for properties.

config file
<configuration>
<appSettings>
<add key="mySetting" value="hello_world" />
...

Code is:
string test = ConfigurationSettings.AppSettings["hello_world"];

Any ideas? test winds up null. I'm going off...

http://www.codeguru.com/Csharp/Csharp/cs_network/configurationfilesinis/print.php/c7987/

Thanks in advance!

Derrick
 
D

Derrick

That was it! Thanks!!!

Derrick

Jon Sagara said:
How did you add the file to your project? Try doing the following:

Right-click on the project and follow the Add popup menu
Click on Add New Item
Under the Local Project Items node, select Utility
Select Application Configuration File and click OK. Leave the file's name
as App.config.

AFAIK, those are the steps you must take to add a config file. I believe
that if you manually add it, it will get deleted every time you build the
project.

- Jon


Derrick said:
I have a C# console app, MyApp, and am placing an application config file
in
the same debug dir as MyApp.exe. I have named the file MyApp.config, and
MyApp.exe.config. I cannot read the <appSettings> entries in the file
though, I get null back for properties.

config file
<configuration>
<appSettings>
<add key="mySetting" value="hello_world" />
...

Code is:
string test = ConfigurationSettings.AppSettings["hello_world"];

Any ideas? test winds up null. I'm going off...

http://www.codeguru.com/Csharp/Csharp/cs_network/configurationfilesinis/print.php/c7987/

Thanks in advance!

Derrick
 
J

Joakim Karlsson

You have mixed up the key and the value. 'mySetting' is the key and
'hello_world' is the value.

Your code should read:
string test = ConfigurationSettings.AppSettings["mySetting"];

Regards,
Joakim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top