The magic of App.config

Posted: (EET/GMT+2)

 

If you have been using C# 2.0 and Visual Studio 2005, the chances are you've heard about "settings". When you add a "Settings.settings" XML file into your WinForms project, and visually edit the values, your application can generated Properties.Settings.Default object to access those properties at run-time.

When using this feature, you might have noticed that also the file App.config changes. And somehow, the contents of this file becomes the YourApp.exe.config file in your project's Bin directory. How does the App.config magically become YourApp.exe.config?

This is something I haven't noticed officially mentioned anywhere byt Microsoft, so I decided I'd give a short summary. Since Visual Studio 2005 uses the MSBuild engine, it is is a ready-made task for the C# compiler that does this file copying for you automatically.

If you go to your project's properties in Visual Studio and enable verbose logging of build events, you can see that a MSBuild target called _CopyAppConfigFile is listed in the log. This is exactly the instruction that copies App.Config to be your application's config file. For example:

copy /y "App.config" "bin\Debug\ConsoleApplication1.exe.config"

Mystery solved!