Reputation: 607
I'm trying to understand Application Settings / AppSettings.
My understanding is that AppSettings (which are within the app.config file) are accessed via ConfigurationManager.AppSettings, and this is considered deprecated functionality.
I understand Application Settings are accessed via Properties.Settings.Default, and are usually created as .settings files (in Visual Studio this can be done in Project Properties).
If this is correct (please confirm), my question is why do all projects in Visual Studio automatically create an App.config file? Can / should I delete this file and create a Settings.settings file instead? Should I use both? (why?) Can I access the app.config file using Properties.Settings? If so, how?
Also, if Properties.Settings.Default accesses the "default" settings file, how / why would you access a file other than the default one?
Edit: This question is not answered by the AppSettings vs Application Settings threads, because my specific question is why I need an App.Config file.
Upvotes: 3
Views: 1948
Reputation: 4136
ApplicationSettings are preferred over AppSettings as they are strongly typed, but there are times (such as in Azure) where AppSettings are preferred as they can be set from the Azure control panel on an instance by instance basis.
That said, let's look at the other part of your question relating to Settings.Default vs. App.Config.
I would not remove the App.Config from every project, as it gets automatically updated when you update your settings. The settings file has the default values in code, but the App.Config file can override those at runtime. This is nice to be able to copy the relevant sections to new applications (into their app.config or web.config) that are going to consume a library.
The only app.config file from your projects that gets executed is the startup project, and the values in that app.config (or web.config as it may be) are the ones used at runtime.
Hopefully this clears up some confusion around the relationship of the files.
Sample Structure of Configuration Files:
ProjectA
- ProjectA.Settings
- App.Config // VS syncs with Settings file
ProjectB
- ProjectB.Settings
- App.Config // VS syncs with Settings file
ProjectC (Startup Project
- ProjectC.Settings
- App.Config
// Can contain override settings for ProjectA.Settings and ProjectB.Settings
Upvotes: 1