Problem using App.cofig

I

inpreet

I am using App.config to read strings. But when ever I try to read
strings in my C# application it always returns null.
My App.config
************************************************************
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSetings>
<add key="default_dir" value="c:\\default\file.txt"/>
</appSetings>
</configuration>
************************************************************
My C# sample for reading keys.
using System.Configuration;

NameValueCollection settings;
string default_dir;
settings = ConfigurationSettings.AppSettings;
default_dir = settings["default_dir"];
*************************************************************
It is returning default_dir is equal "null" but it should
"c:\\default\file.txt" .
 
R

Richard Blewett [DevelopMentor]

inpreet said:
I am using App.config to read strings. But when ever I try to read
strings in my C# application it always returns null.
My App.config
************************************************************
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSetings>
<add key="default_dir" value="c:\\default\file.txt"/>
</appSetings>
</configuration>
************************************************************
My C# sample for reading keys.
using System.Configuration;

NameValueCollection settings;
string default_dir;
settings = ConfigurationSettings.AppSettings;
default_dir = settings["default_dir"];
*************************************************************
It is returning default_dir is equal "null" but it should
"c:\\default\file.txt" .

Just to be clear. app.config is a Visual Studio thing. VS copies this file
to the build target directory (e.g. bin\debug) as <appname>.exe.config. This
file is the one that .NET automatically picks up. So if you have an
application called foo.exe the config file is called foo.exe.config

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
I

inpreet

I have readfile.exe and I tried my config file name as
readfile.exe.config and readfile.config with build action as none as
well as content. but nothing is working. Still I am getting null value.

*******************readfile.exe.config ****
readfile.config*************************
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="default_dir" value="c:\\default\file.txt"/>
</appSettings>
</configuration>
****************************************************************************************
 
M

Marc Scheuner [MVP ADSI]

<appSetings>

That should read <appSettings> with two "t" in the mdidle there. Maybe
that typo is causing your problem?

Marc
 
M

Marc Scheuner [MVP ADSI]

I have readfile.exe and I tried my config file name as
readfile.exe.config and readfile.config with build action as none as
well as content. but nothing is working. Still I am getting null value.

You have to call the file "app.config" (literally that - DO NOT
substitute "app" for your app's name!!) - and then VS.NET will
automagically rename it to "readfile.exe.config" and copy it to your
output directory. That's the way to go.

Marc
 
K

Kevin Spencer

Have you tried

default_dir = (string) ConfigurationSettings.AppSettings["default_dir"];

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.
 

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