2

If I create an app.config file in a console apps like this

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

and access the same from the console application like

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

or by

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

I am able to get the value.

But if I do the same thing in a class library project, I am getting a null value. Why? Where I am making mistake? I have added the proper reference System.Configuration. I am using C# 3.0

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
Newbie
  • 1,111
  • 3
  • 17
  • 36

2 Answers2

1

You should define your configuration in the main project configuration file where you're adding your class library.

In that way you can access all key values in your class library by adding those keys value in your main project configuration file.

best of luck.

Matteo Alessani
  • 10,264
  • 4
  • 40
  • 57
aishraj
  • 11
  • 1
0

.NET has no built-in support for class library configuration files. Ofcourse, with some trickery you could manage this but the support was left out by design.

Sandor Drieënhuizen
  • 6,310
  • 5
  • 37
  • 80