2

I am working on a project(web app) and referenced a class library to it, created config file and added values to it, now I need to read values from app.config from the class library.

is there a way to do it?

I researched but could not find anything.

  • _"I researched but could not find anything"_ - show _what_ you researched, as this question has been asked many times before. – CodeCaster Feb 20 '18 at 09:05

2 Answers2

2

You would typically only want one config file. If this is a web application, it will be the web.config file. You can read this from a class library by adding a reference to System.Configuration and using ConfigurationManager.AppSettings.Settings["settingkey"] to access it.

If you really need to access a separate app.config file, you will need to know the path to it and use something like:

ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = pathToAppConfigFile;
System.Configuration.Configuration config = ConfigurationManager
    .OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
setting = config.AppSetting.Settings["settingkey"];
Steve Harris
  • 5,014
  • 1
  • 10
  • 25
0
ConfigurationManager.AppSettings 

is the way to go. See this link for further reference and functioning example: https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(v=vs.110).aspx

A. Non
  • 179
  • 11