Generic app.config for multiple applications

I have several console applications in C # that should have the same set of settings. I want to avoid duplicity and avoid a separate app.config for each application.

Is there a way to read the common app.config file (say common.config ) for applications ( app1.exe , app2.exe ).

+6
source share
3 answers

You can download the external app.config file using the following code:

 config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe")); string logpath = config.AppSettings.Settings["Log.Path"].Value; 

And save the settings like this:

 config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe")); config.AppSettings.Settings["Log.Path"].Value = "C:\newpath"; config.Save(); 

You may need to have the main configuration in one of the applications and specify the rest. This method is usually considered bad practice. There may be problems with various applications blocking the file.

+5
source

Create one file called app.config. Put it in some place outside the directories of your projects, for example, in the catalog of solutions. Add it to your projects as a related element with a relative path to the file. Set the correct assembly action for this item (application configuration) in each project.

Now, when each project is created, the file will be copied to the project output file with the correct name.

+8
source

@ Answer is an option, but each application will still have its own configuration file after build. At compile time they will be the same, but during deployment they will be copies.

You can also open one application configuration file from another application using: ConfigurationManager.OpenExeConfiguration(string)

You may have an external configuration file that all applications link to: ConfigurationManager.OpenMappedExeConfiguration

And there is the possibility of using the machine configuration file using: ConfigurationManager.OpenMachineConfiguration()

+4
source

All Articles