Problem reading config file from class library project

If I create a file app.config

in console applications like

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key ="key1" value ="val1"/>     
  </appSettings>
</configuration>

      

and access the same from a console application like

object sourcePath = System.Configuration.ConfigurationManager.AppSettings["key1"];

      

or

object sourcePath = System.Configuration.ConfigurationSettings.AppSettings["key1"];

      

I can get the value.

But if I do the same in a class library project, I get a null value. What for? Where am I going wrong? I have added the correct link System.Configuration

. I am using C # 3.0

+2


a source to share


2 answers


.NET does not have built-in support for class library configuration files. Sure, with some trickery, you could handle this, but support has been overlooked by design.



0


a source


You have to define your config in the main project config file where you add your class library.

This way you can access all the key values ​​in your class library by adding these key values ​​to your main project config file.



Good luck.

+1


a source







All Articles