How to use multiple App.config

Asked

Viewed 917 times

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 by System.Configuration.ConfigurationManager.AppSettings the key is searched in the Startup project App.Config and not in the project where the call is being made.

  • And that’s right, this is the expected behavior.

  • If a project is shared among several solutions, this project cannot have its own configuration file?

  • 1

    Yeah, I’m posting an idea for you

  • 1

    Yes, @LINQ is posting an idea p/ you.

1 answer

3


This is the expected behavior.

You may have separate configuration files, but you will need to read them manually, the two methods cited by you will capture the information from the Assembly that is being executed.

You can use the method ConfigurationManager.OpenExeConfiguration to capture an object of the type Configuration and retrieve data saved on appSettings his.

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");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.