Wednesday, 25 April 2012

How to read the conig file of class library in C Sharp

Every body knows that we can define the appsettings in the app.config file of class library. When we are refering the existing class library to the other new class library that only takes dll but not the configuration file of existing class library. To read values from the configuration file one option is to define same appsettings in the new class library. the other solution is as follows.

Solution:

  1. Refer the class library to the other new class library.
  2. Copy the .dll.config file from bin/debug folder of the existing class library and add to the new class library.
  3. Use the following code to get the values from the .dll.config file.
/// Load the .config file for the current DLL
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();

Configuration dllConfig = ConfigurationManager.OpenExeConfiguration(assembly.Location);

KeyValueConfigurationCollection settings = dllConfig.AppSettings.Settings;
//read the value as follows
string value=settings[key].value;

No comments:

Post a Comment