Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using app.config with a class library

Frequently I need to create a .Net class library that requires an app.config for things such as database connection strings. However, these settings must be in the calling application's app.config or web.config. If I want to distribute the DLL across multiple applications it becomes a pain because I have to copy these settings into each the application's app.config.

I have considered manually reading the config settings via code from inside the class library, but that is also a major pain. Does anyone have any suggestions for the best way to load app.config settings inside a class library?

like image 671
Billkamm Avatar asked Mar 27 '09 15:03

Billkamm


People also ask

Can we have app config in class library?

No, class libraries can hold setting files, but their values will be defined in the application configuration (web. config, app.

When should I use app config?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.

How read app config file in C# Windows application?

Add the App. config file to your project. After creating a . NET Framework project, right-click on your project in Solution Explorer and choose Add > New Item. Choose the Application Configuration File item and then select Add.


2 Answers

I know it's not what you want to hear, but the entry point's config file is the logical place to put these setttings. If every library required its own config file then it would get tedious to have to edit every single file instead of just one.

I would have a text file with the default settings included as part of your library (and as part of the project) which can be distributed with the library for the default settings configuration (it can be copied and pasted into the config file).

like image 198
casperOne Avatar answered Oct 06 '22 18:10

casperOne


One thing you could do is to have a seperate settings file just for your library, then you only have to have a reference to it in your entry apps config file.

See the accepted answer in this question for information on this.

like image 5
AaronS Avatar answered Oct 06 '22 17:10

AaronS