Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 641
PramodChoudhari Avatar asked Jun 17 '11 05:06

PramodChoudhari


People also ask

Can a DLL have app config file?

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.

Where are app config files stored?

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.

Can class library have app config?

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.

Why DLL config is created?

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.


1 Answers

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.

like image 88
Davide Piras Avatar answered Sep 19 '22 13:09

Davide Piras