Very often I see the answer to the question like: "How should I store settings in my .NET app?" is to edit the app.config file by manually adding entries to the app.config (or web.config) like so:
<configuration>
<appSettings>
**<add key="ConfigValueName" value="ABC"/>**
</appSettings>
</configuration>
Then, accessing them like:
string configValue = Configuration.AppSettings["ConfigValueName"];
I'll refer to the approach outlined above as the "app.config" approach. Very rarely do I see people recommend adding a "Settings" file to the project. I've seen this so many times across the web and on stackoverflow... I'm starting to wonder if i'm missing something... because I'm not sure why you'd use this method over using a "Settings" file. I didn't get into .NET until VS2005 so one theory I have is this was how things were done in VS2003 and people never switched over?
Examples of people recommending the app.config approach:
- http://stackoverflow.com/questions/114527/simplest-way-to-have-a-configuration-file-in-a-windows-forms-c-application
- http://stackoverflow.com/questions/535685/best-practices-how-should-i-store-settings-in-c-format-type
From my viewpoint there are following advantages of the "Settings file" approach:
- Can be used for both Application Settings (those that are common to all users) and User settings from the same interface.
- Ability to use the Settings designer support in visual studio. Less error prone then editing an XML file directly IMHO.
- Refactoring - you can rename a particular setting name and it will automatically update references in your code.
- Compile type checking.
- Auto-completion support.
- Property-Grid Capabilities. I have found that the PropertyGrid control is a very easy way to make a quick options form. You just do
propertyGrid1.SelectedObject = Settings1.Default;
and you're done.
In case you are not sure what I mean by the "Settings" file approach see this post which is one of the few examples where someone recommends using Settings files instead of app.confg.
EDIT: Please understand: The intention of this topic is to figure out why people would use the app.config approach outlined above over the Settings file approach. I have encountered the limitations of the Settings file approach and have been forced to roll my own custom solution at times. That's an entirely different discussion.