Teemu
Teemu

Reputation:

Sharing application preferences across multiple projects

We have a fairly large .NET solution with multiple executable projects (winforms and command line programs). Currently each of these projects has its own app.config that contains connection strings, mail server settings and the like. As you can imagine it's not that convenient to make a change in every app.config file whenever a particular setting needs to be updated.

What do you reckon is the best practice for centralized management of these settings? So far I've thought about two different approaches. The first is using a custom class (or classes) that contains the settings and is serialized into and deserialized from XML. The second approach is defining an app.config file only for a single project and using ConfigurationManager.OpenExeConfiguration() to access it.

Upvotes: 2

Views: 1743

Answers (3)

technophile
technophile

Reputation: 3676

Use the ConfigSource directive to have all of the settings loaded from a central file with the shared values.

Upvotes: 1

Leahn Novash
Leahn Novash

Reputation: 2941

Create the App.config on your startup project and link statically to it on the other projects.

You can link statically by going to Add->Existing Item, and when clicking the Add button on the File Browser Window, there is a small down arrow on the Add button. Click there and the "Add as link" option will be shown. That will place the same App.Config on both projects, with only one actual file.

If your projects have different App.Config with only 'some' settings that are the same, consider checking the file parameter; I mean, you can have more than one App.Config in a project: http://weblogs.asp.net/pwilson/archive/2003/04/09/5261.aspx

Just create a central common.config file and point to it.

Upvotes: 1

efdee
efdee

Reputation:

Personally I would advise against introducing a needless dependency on a .config file from another assembly. Using a custom class with serializing sounds cleaner in this scenario, but you expose yourself to potential versioning problems and you lose the potential advantages offered by app.config (section handlers, etc.).

Upvotes: 0

Related Questions