Newbie help with .config

G

Guest

Hello all

Can't seem to get 'System.Configuration.ConfigurationSettings.AppSettings' to work. What I've done thus far, is jus
create a windows app called 'WindowsApplication2'. To this app I used 'Add new Item' and added 'Application configuratio
file' and named it 'WindowsApplication2.exe.config'. I then rebuilt the solution. After rebuilding I went to the folder wher
my project is and copiled the .config file into the.bin\debug and tried run to cursor to see if I could read the .confi
file. Nope. Get an error

using System.Configuration

static void Main()

string test1 = System.Configuration.ConfigurationSettings.AppSetting
["Key1"].ToString()

MessageBox.Show(test1)

Application.Run(new Form1())


The .config looks like this

<?xml version="1.0" encoding="utf-8" ?><configuration><appSettings><add key="Key1" value="One" /><add key="Key2" value="Two" /><add key="Key3" value="Three" /></appSettings></configuration

When I run to "string test1.." and try to step over I get a
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication2.ex
Additional information: Object reference not set to an instance of an object

Help, what am I missing, doing wrong,etc.

Any help appreciate

TIA

Ray K
 
W

William Ryan eMVP

Raykos:

I took your .config file directly from your code b/c I couldn't find
anything wrong with it. I Created a new C# PRogram, added a Configuration
file, pasted this over the existing empty config settings then put your code
referencing it behind a button_click and used a MessageBox.Show(test1);

It works fine. Then I moved it into Main before the call to Application.Run
so it is exactly like your code and I still didn't have any problem.

So, the only thing I can think of is wrap that code in an exception handler
and see use ex.ToString() to see if you can determine anything.

However, All I did was add the config file from VS.NET, it takes care of the
rest. I didn't set its name or copy it anywhere. Delete the file in your
bin folder and the compile again, it should rebuild the exe.config file for
you. I'm thinking maybe it can't read it for some reason. Anyway, it
should work so just delete the file from Bin and recompile..You should be
good to go. BTW, I've deleted my exe.config and then rebuilt and it works
fine so you should be alright.

HTH,
Bill
raykos said:
Hello all,

Can't seem to get
'System.Configuration.ConfigurationSettings.AppSettings' to work. What I've
done thus far, is just
create a windows app called 'WindowsApplication2'. To this app I used 'Add
new Item' and added 'Application configuration
file' and named it 'WindowsApplication2.exe.config'. I then rebuilt the
solution. After rebuilding I went to the folder where
my project is and copiled the .config file into the.bin\debug and tried
run to cursor to see if I could read the .config
file. Nope. Get an error.

using System.Configuration;

static void Main()
{
string test1 = System.Configuration.ConfigurationSettings.AppSettings
["Key1"].ToString();

MessageBox.Show(test1);

Application.Run(new Form1());
}

The .config looks like this:

<?xml version="1.0" encoding="utf-8" ?><configuration><appSettings><add
 
W

William Ryan eMVP

Glad it worked ;-)
raykos said:
Hello William,

Thanks for responding. I did exactly what you did, and like you, it
worked!? The only thing I did differently
was not to change the name when I added the config file; most of the
documentation I've been reading indicated
 

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