Is there an equivalent to app.config for libraries (DLLs)? If not, what is the easiest way to store configuration settings that are specific to a library? Please consider that the library might be used in different applications.
Is there an equivalent to app.config for libraries (DLLs)? If not, what is the
Share
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.Assuming you’re using Visual Studio as your IDE, you can right click the desired project → Add → New item → Application Configuration File
This will add
App.configto the project folder, put your settings in there under<appSettings>section. In case you’re not using Visual Studio and adding the file manually, make sure to give it such name: DllName.dll.config, otherwise the below code won’t work properly.Now to read from this file have such function:
And to use it:
You’ll also have to add reference to System.Configuration namespace in order to have the ConfigurationManager class available.
When building the project, in addition to the DLL you’ll have
DllName.dll.configfile as well, that’s the file you have to publish with the DLL itself.Within the VS project, you should set the .config file "Copy to output directory" setting to "Always Copy".
The above is basic sample code, for those interested in a full scale example, please refer to this other answer.