Including a configuration file from a class library

I have a configuration file in my C # class library called MyLibrary.config in version 2008.

I created another project, say, a simple console application, add the View link to MyLibrary.dll in the bin directory of the class library project, and when I compile, MyLibrary.config will not be included in the output bin directory in the console application.

How can I install it to enable it when I refer to the dll?

Greetings

+4
source share
3 answers

You can not. Your console application expects to find the configuration file with the prefix just like the name of the console application ( MyConsoleApplication.exe MyConsoleApplication.exe.config .).

In some situations, you can share the configuration file using the file attribute in the appSettings element:

 <appSettings file="path"> </appSettings> 

Note that path refers to the running assembly.

As an additional note, DLLs do not even use the configuration file that you defined in the project. Again, the configuration information is read from the configuration file with the prefix in the same way as the executing assembly. Thus, even when MyLibrary.dll tries to pull the configuration information from the configuration file, it will read the configuration file for the executing assembly, not MyLibrary.dll.config .

For more information on how configuration files work, see MSDN .

+5
source

The standard way to use the configuration file is to make it the same as the executable, adding a link to the DLL will not include its configuration file, and as far as I know, the dll does not load the configuration files on its own, rather they rely on the executable which refers to them.

+2
source

Besides the fact that I could not do this, I would also advise against this approach.

Instead of trying to connect your settings to a DLL, consider a more “Dependency Approach” approach — that is, where you pass the value dependencies (that is, the settings) to the code you call (that is, in the library).

The advantage of this is that you are not tied to one way of saving settings. Then you can use the database, different configuration file formats ... even hard coding. It also simplifies unit testing by removing the external dependency of a file.

There are various ways to implement this, however one example is to pass configuration parameters to the constructor of the class (s) that uses them.

0
source

All Articles