know what the app.config is for and how to use it. Now you might be wondering what happens behind the scenes. Well, when you compile your application, the compiler actually copies the app.config file to the output folder, but gives it another name:
When you start your application (ConsoleApp1.exe in our example), the matching config file will be loaded too.
It is possible to change this file while the application is running, but you will need to restart the application for the changes to take effect. Luckily, there are ways around this:
- A call to ConfigurationManager.RefreshSection(“…”) will reload the given section
- For applicationSettings, you will need to call Properties.Settings.Default.Reload();
Keep in mind that a new compilation or deployment can overwrite the changed values. So it’s better to make the changes in your source code (which is under source control, right?) and redeploy.
You should consider not putting confidential information in your config files. Things like passwords and API keys should be kept out of the app.config file. In an age where more and more code is made open source, you could risk exposing your secrets to the outside world. Even if your repository is private, it’s a best practice to put the secrets elsewhere. First, if you find it normal to do so, you won’t do it when you’re working on a public and open source project. But second, you never know when your private repository may become public, on purpose or by accident.
You can easily solve this by putting your secrets in separate files:
See how both the appSettings and the connectionStrings point to another file? Also note that we can combine an external file with appSettings, but for connectionStrings we need to choose.
If we make sure we don’t put these files under source control, they can remain absolutely private. Another thing to remember is to tell the compiler to copy these files to the output directory:
After that, you can use the appSettings and connectionStrings in your code like you did before.
A Final Tip
I want to revisit the point I made about the appSettings not being type-safe. If you have multiple places where you access your appSettings, you might not like having to repeat the (string) key everywhere, and having to convert the string value to the correct type every time. A simple solution to this is to create your own class that wraps the call to ConfigurationManager:
In all other locations of your code, you can now call this class and be sure you’re getting an integer.
This article aimed at showing you the basics of the app.config file, and some best practices. The app.config file is a basic piece of the .NET Framework, yet I’ve seen several projects putting their configuration in other places (like plain text files or the registry). Unless you have a very good reason to do so, it’s more convenient and familiar to use the app.config file. You can change it easily, the .NET Framework supports it out of the box and almost all .NET developers know how it works.