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:
From my viewpoint there are following advantages of the "Settings file" approach:
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.
App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.
Configuration namespace to read settings from the configuration files, but not to write settings to those files. This article describes the syntax of configuration files and provides information about the three types of configuration files: machine, application, and security.
AWS AppConfig simplifies the administration of applications at scale by deploying configuration changes from a central location. AWS AppConfig supports configurations stored in Systems Manager Parameter Store, Systems Manager (SSM) documents, and Amazon S3.
A configuration file (web. config) is used to manage various settings that define a website. The settings are stored in XML files that are separate from your application code. In this way you can configure settings independently from your code.
I believe that the biggest difference between the two is that the application cannot change the values in app.config
. Those values are read at runtime and there's no built-in support for writing new values to the configuration file.
Settings files can be changed using the Save()
command.
One major problem with the built-in support for Settings files is where the settings file is stored. If you look at your APPDATA folder, you'll see that there is a folder for the name of the company, then a sub-folder with the name of the product, then a sub-folder with a semi-random name and version info.
Whenever you release a new version, it won't locate the Setting file from the previous version because of where the Setting file is stored. There's also no way to change where the Setting file is stored.
I used it in one project and found it much more useful to create my own AppSettings class that uses an XML file for settings. I have control over the format and location of the file.
There is a third, and much, much better way than either AppSettings or Properties: custom configuration sections. Advantages over AppSettings:
1) Strongly typed access
2) Built-in validation mechanisms for both the schema and the form.
3) POCO based making it highly testable--just new up your own test config.
4) No reliance on magic strings. Not that you can't easily wrap AppSettings in a class and access it that way, but 95% of developers don't.
5) XML in the configuration becomes much more intelligible once you get to a dozen or so settings. Also much more intelligible than the Propterties bits IMHO.
6) No reliance on visual studio to make it work well.
Granted, I never really used properties because I got my hands on custom configuration sections first.
BTW, if you haven't heard of these, you are still using them every day--what do you think the standard config sections in the .NET configuration files are?
_____CLAIRIFICATION SECTION
First, the bulk of this was aimed towards the AppConfig approach, and re-reading I wasn't clear. I think we're generally on the same side--avoid the loose name-value bag for configuration because it breaks too easily. I should also add that I am a web guy, so user settings are something that lives in the application layer, not in config, to me.
1) True, both Properties and custom config sections have strongly typed access. AppSettings don't. AppSettings bad, Props/CC good.
2) When you load a configuration section, the application will throw a configuration exception if it does not expose necessary information or improper information. Same as when you muck up a app.config or web.config. There is a bit more validation infrastructure I haven't used because I like to fail hard and early or not at all.
3) POCO is plain old C# object in case anyone missed the last few years. Anyhow, because config settings are decorative and declared via attributes, your config settings can be easily tested because you just need to delcate a new MyConfigSection() and set properties, etc, as appropriate. As I understand properties, you need to have the configuration file there with the right xml in it or you are sol. Other advantage is custom config sections work with all the other neat stuff you can do with POCOs, like interfaces and dependency injection.
4) True, both Properties and custom config sections are strongly typed, AppSettings are not. AppSettings bad, Props/CC good.
5) Really aimed at the AppSettings. I've inherited a few apps with litterally 2 pages of <add name="foo" value="bar" />
in the config. That is easy compared to the xml jibberish that properties spit out. On the other hand, your custom config section can be rather semantic and self-explanitory because you are declaring the section names and attributes. I can pretty easily edit it in notepad on the production server and not be too nervous about shooting myself in the foot in a pinch.
So, outside of not having a VS-based property grid (which I would contend is a bad thing--why do you need a grid to edit configuration?), custom config sections have alot of advantages and no real disadvantages compared to Properties. Both custom config sections and properties have massive advantages over AppSettings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With