i am having a classlibrary with app1.config and also a windows application with app2.config;i am adding the reference of classlibrary in windows application as well as app1.config.is it possible if i call the method class lib it will go to app1.config otherwise it will use app2.config;
-
1possible duplicate of [Using app.config with a class library](http://stackoverflow.com/questions/690313/using-app-config-with-a-class-library) and a great many other SO questions - please **SEARCH** first before posting the gazillionth repeat of the same question! – marc_s Dec 29 '10 at 14:38
-
1Or duplicate of this: http://stackoverflow.com/questions/4277635/reading-an-app-config-in-class-library – marc_s Dec 29 '10 at 14:39
-
1Or duplicate of this: http://stackoverflow.com/questions/2548726/connection-string-in-app-config-in-a-class-library or this: http://stackoverflow.com/questions/2946614/why-is-this-class-library-dll-not-getting-information-from-app-config – marc_s Dec 29 '10 at 14:40
-
if u all think u r very intelligent then y u r not giving the answer and i think u need the check the all previous answers they are not well explained. – slash shogdhe Dec 29 '10 at 16:05
2 Answers
Using the default ConfigurationManager process to access a configuration file it goes for the file that is configured for the application, there is no way to differentiate between a class library and an application's config.
For example if you have a windows/WPF app that is called MyWonderfulApp.exe, the only config file that will be used is MyWonderfulApp.exe.config. Therefore all settings are in that file. Web applications ONLY use the web.config file.

- 62,228
- 14
- 110
- 173
The best you can achieve is to have two separate configuration files, then have the code of one method read the "main" config file (using the ordinary ConfigurationManager.AppSetting[""]
code) and other method read the configuration file of the class library using such code:
Configuration config = ConfigurationManager.OpenExeConfiguration(dllFilePath);
KeyValueConfigurationElement element = config.AppSettings.Settings[appSettingKey];
string value = element.Value;
This will read application setting from the config file of DLL sitting in the location of dllFilePath
.
You can also have separate section for the class library in the "main" configuration file if relevant I can give sample as well.

- 66,030
- 26
- 140
- 208