Search code examples
c#configurationmanager

ConfigurationManager loaded within the project itself


I have a app.config file loaded with some values in a project, which i load by

var value = ConfigurationManager.AppSetting["myKey"]

It works fine if I debug from the project itself, but if I create a new Console Project within the same Solution, then the AppSettings[] seems empty.

Is it possible to specify that my ConfigurationManager ALWAYS should point to the app.config file in the project where the ConfigurationManager is used?


Solution

  • ConfigurationManager always reads the app.config file from the executing assembly regardless of which assembly it is called from.

    So in your case you should add the app setting to console projects app.config file and then even though the call to ConfigurationManager is not being made from within the console project executable itself, the value will be read from its app.config file.