App.config And Web.config, The Two Configuration Files Story
Posted by triaslama on November 12, 2008
In .NET programming world we govern setting for our program through configuration file (file with .config extension). We use web.config for web application (ASP.NET) and app.config for the rest.
Although both of config files share much similarities but both has difference too. Yes, I realize that many of you maybe already know what is the difference, but I wrote this because just a moment ago I realize one of the difference.
In ASP.NET we can just drop our web.config file in root of our virtual directory and this file will be referenced automatically, as simple as that. When we use app.config file -for example in Windows Forms application- the app.config file need to renamed with the same name as our executable file (or *.dll file for library). In visual studio this thing happen automatically, so when I try it manually (without any of IDE) the problem arise.
I write a very simple Winforms application (lets say I give this Winforms app. as test.cs), I write app.config file too then I place this app.config in the same directory with my simple Winforms program. Inside my Winforms program(test.cs) I retrieve a value from app.config file. Then I compile my source code manually (with csc command, so it will produce test.exe) and I run it. Unfortunately I get NullReferenceException! “Oh no, what was already happen. I think I’m not do something strange” maybe something like that I muttered.
Yes, I am not do something wrong except because I retrieve value from app.config! Suddenly I realize that my app.config cannot referenced by my executable file (test.exe) automatically (like what will happen in web.config case). Because I feel it’s strange (It work when I use Visual Studio but doesn’t work when I manually compile it) I inspect my Visual Studio files and I open bin directory. Finally I found this: the config file (app.config) renamed automatically by Visual Studio with the same name with my *.exe file, but with *.config extension.
Oh, so this is the matter. the *.exe file (or *.dll file) referenced config file automatically as long as the config file has the same name. So I rename my app.config become test.config, run again my test.exe and yes now it works.