AppSettings does not work
I am writing a C# windows program with Visual Studio 2003. I created the app.exe.config file and entered the following:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DBConnect" value="server" />
</appSettings>
</configuration>
But when I try to retrieve the value with the following:
string sConnect;
sConnect = ConfigurationSettings.AppSettings["DBConnect"].ToString();
I get an error message:
Object reference not set to an instance of an object
I am in Debug mode and I cannot see or copy the config file to the bin\debug folder. Does this matter or does VS need to the add the file?
Why can't I run the code?
[773 byte] By [
PineTree] at [2007-11-11 7:50:53]

# 1 Re: AppSettings does not work
Is the file actually named "app.exe.config"? It needs to be named [name of your program].exe.config, and it must be in the same directory as the executable. That is, if your prorgam is called MyProgram.exe, the config file must be MyProgram.exe.config, and must be in the same directory as MyProgram.exe.
# 2 Re: AppSettings does not work
I am running in debug mode in VS2003 and I do not see the app.exe.config being created in the bin\debug folder with the app.exe file. I started the app by pressing the F5 key and trap at the spot where I call the AppSettings["DBConnect"] code line. Then I looked in the bin\debug directory with explorer and the config file is not there.
# 4 Re: AppSettings does not work
Please comment on the following as I finally got this to work:
1 When adding the new item (Application Configuration), the name of the file MUST BE app.config. Any other name such as App1.config (the default name) or myApp.exe.config WILL NOT WORK.
2 When the project is run by pressing the F5 key, the VS program will copy the app.config file, rename it to myApp.exe.config, and place the file in the bin\debug folder where it will be detected.
Is this a bug on my computer? Or is this normal?
Thanks for your patience as I have wrestled with this problem for months.
# 6 Re: AppSettings does not work
OK. It looks like you add the config file to the released application directory which works for stand-alone files that are released. But how do you test the config file works from the debug mode of Visual Studio environment?
# 8 Re: AppSettings does not work
I did that too, but when I run the program, VS erases my config file from the folder and attempts to create its own file. If the name is myApp.exe.config in my list of classes, then NO FILE IS WRITTEN.
Anyway, I thank you for your help as I have taken much of your time and I quess I will use my work around that I described. I also should move on to VS 2005! Thanks again.
# 9 Re: AppSettings does not work
What if you don't have an app.config in your project? Note that I am not suggesting you do things my way -- letting VS manage the file sounds easier. I'm just explaining how I've always done it and thus why I didn't know about the automatic features of VS. Glad you got it figured out!