Pros and cons of AppSettings vs applicationSettings (.NET app.config / Web.config)
The basic <appSettings>
is easier to deal with - just slap in a <add key="...." value="..." />
entry and you're done.
The downside is: there's no type-checking, e.g. you cannot safely assume your number that you wanted to configure there really is a number - someone could put a string into that setting..... you just access it as ConfigurationManager["(key)"]
and then it's up to you to know what you're dealing with.
Also, over time, the <appSettings>
can get rather convoluted and messy, if lots of parts of your app start putting stuff in there (remember the old windows.ini file? :-)).
If you can, I would prefer and recommend using your own configuration sections - with .NET 2.0, it's really become quite easy, That way, you can:
- a) Define your configuration settings in code and have them type-safe and checked
- b) You can cleanly separate YOUR settings from everyone else's. And you can reuse your config code, too!
There's a series of really good articles on you to demystify the .NET 2.0 configuration system on CodeProject:
Unraveling the mysteries of .NET 2.0 configuration
Decoding the mysteries of .NET 2.0 configuration
Cracking the mysteries of .NET 2.0 configuration
Highly recommended! Jon Rista did a great job explaining the configuration system in .NET 2.0.
Application settings can be controlled from a designer (there is usually a Settings.settings file by default) so its easier to modify and you can access them programmatically through the Settings class where they appear like a strongly typed property. You can also have application and user level settings, as well as default settings for rolling back.
This is available from .NET 2.0 onwards and deprecates the other way of doing it (as far as I can tell).
More detail is given at: msdn.microsoft.com/en-us/library/k4s6c3a0.aspx
To understand the pros and cons of settings in the app.config
, I suggest that you look into the technical details of both. I have included links where you can find source code for handling, describing more technical details below.
Let me briefly summarize what I recognized when I worked with them (note: the same is applicable to the web.config
file of a web site / web application):
applicationSettings in .NET
(click above to view source code and technical details)
Pros
They allow to store typed data, including object types (via
serializeAs
property)They have a user and application scope, allowing to store default values
They are supported in Visual Studio's configuration section
Long strings and/or data with special characters are very well supported (for example, embedded JSON strings containing double quotes)
Cons
User settings are stored in a different place in the user profile (with a cryptic path), can be difficult to clean up
Application scope settings are read-only during runtime of the application (only user scope settings can be changed during runtime)
Read / Write methods code built by Visual Studio's settings designer, not directly provided by 3rd party tools (see link above for a workaround solution)
AppSettings in .NET
Update: AppSettings in .NET Core
(click above to view source code and technical details)
Pros
Are "light-weight", i.e. easy to handle
Read and write access during runtime of the application
They can be edited easily by Administrators in the
Internet Information Services (IIS) Manager
(Features View -> Application Settings, note that the name of the icon is misleading since it can only handle AppSettings and not ApplicationSettings)
Cons
Support only string data; string length and special characters are limited
They don't have a user scope
They don't support default values
Are not directly supported in Visual Studio's configuration section