How to read app.config at dll level.? [duplicate]

2019-04-19 08:44发布

问题:

This question already has an answer here:

  • C# DLL config file 16 answers

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?

回答1:

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.