简体   繁体   中英

C# app.config sharing between multiple projects

I am working on a solution that has multiple projects.

I follow the method described here (the first response that suggest using a common solution item): single app.config multi-project c#

The result is that for each project, the app.config file is copied and renamed like XXX.exe.config.

However, the desirable output is that the config file name remain the same (eg App.config) for each project. Because I wanted to put all the executable and the DLLs in the same folder when it's deployed (some executable reference to the same DLL so it make sense to put them all together).

Any suggestion is appreciated.

You can load a configuration file manually. See the discussion here: Using ConfigurationManager to load config from an arbitrary location

I suggest you look at using TT templating. Then the simple solution is to have an App.tt file that includes a common ttinclude file. This will generate an App.config file with the same (or different) content based on other settings you may want to set in the app.config.

For example we have an App.Config that looks like this:

<# EnvironmentName = "local"; #>
<#@ include file="App.Template.ttinclude" #>

The App.Template.ttinclude contains everything we want to output (along with some settings that change based on the Environment value as we support 8 installation enviromments.

Each App.Template.ttinclude actually includes another master config file that contains our common settings, but that starts to get complicated as we include environment-specific configs within the master config file, in a switch statement (to get around a bug in TT file processing).

Where we have an app specific DLL config, it simply includes one of our others:

eg MyApp.Exe.config contains:

<#@ include file="App.tt" #>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM