I have a console application project and library project (dll) in one solution.
The library project has app.config file where I store some key value pair that I use in library. The console application references this dll.
I have another app.config file at console application where I have settings for console application.
When I run the program my library.dll always refers app.config from console application.
I want to separate the responsibility of each dll, so I want a dll to always refer to its own app.config file not that of the calling application.
In this case library.dll should use app.config of its own, not of the calling application.
how do I achieve this?
You can have separate configuration file, but you'll have to read it "manually", the ConfigurationManager. AppSettings["key"] will read only the config of the running assembly. This will add App. config to the project folder, put your settings in there under <appSettings> section.
The file is stored inside this path "bin/debug/app. config", if you make changes while debugging, those changes should appear there. Just remember that this file is overwritten with the "app. config" from the project root each time you run the application on Visual Studio IDE.
Class libraries can access configuration settings in the same way as executable apps, however, the configuration settings must exist in the client app's App. config file. Even if you distribute an App. config file alongside your library's assembly file, the library code will not read the file.
The reason dll. config exists at all is because VS doesn't distinguish between libraries and EXEs when it needs to generate config settings. Therefore if you happen to be inside a library and VS needs to generate a config entry it'll generate it in a dll. config but that file will never be used at runtime.
What you are asking has been asked many times again and again...
in fact you could instruct .NET to read configuration settings from another file, in your case will be something like library.dll.config, there are projects or classes to do this called something like assemblysettingsreader or similar.
My suggestion is anyway to do not do it. It sounds cool initially but think in this way, your library is not running alone, can be referenced by a console app, a web site or a wpf ui project, all these clients might need to customize the configuration and have different settings so it makes full sense to have the settings related to the calling application config files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With