How to use multiple App.config

5

How to use more than one App.Config? I have a Solution with several projects. In each project there is an App.Config. When I try to read a key, either by AppSettingsReader or System.Configuration.ConfigurationManager.AppSettings the key is searched in the App.Config of the Startup project and not in the project where the call is being made.

    
asked by anonymous 23.08.2017 / 19:08

1 answer

3

This is the expected behavior.

You may have separate configuration files, but you will have to read them manually, the two methods you are quoting will capture information from the assembly configuration file you are running. >

You can use the ConfigurationManager.OpenExeConfiguration to capture an object of type Configuration and fetch data saved in appSettings of it.

Something like:

public string GetAppSetting(Configuration config, string chave)
{
    var element = config.AppSettings.Settings[chave];
    return element?.Value ?? "";
}

The use would be like this

string configLocal = this.GetType().Assembly.Location;
var config = ConfigurationManager.OpenExeConfiguration(configLocal);

string myValue = GetAppSetting(config, "chave");
    
23.08.2017 / 19:29