4
votes

I have a config file in my C# class library called MyLibrary.config, in vs 2008.

I created another project, say a simple console app, add reference by "Browsing" the MyLibrary.dll in the bin directory of the class library project, and when I compile, the MyLibrary.config is not including in the bin directory of the output in the console app.

How can I set it so I can include it when I reference the dll?

Cheers

3

3 Answers

5
votes

You can't. Your console application is expecting to find a config file with prefix the same as the name as the console application (MyConsoleApplication.exe -> MyConsoleApplication.exe.config.).

In some situations you can share a config file by using the file attribute on the appSettings element:

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

Note that path is relative to the executing assembly.

As a side note, DLLs do not even use the config file that you've defined in the project. Again, configuration information is read from the a config file with prefix the same as the executing assembly. Thus, even when MyLibrary.dll tries to yank configuration information out of a config file, it will be reading the config file for the executing assembly, not MyLibrary.dll.config.

For more on how config files work, see MSDN.

2
votes

The standard way to use a config file is to have it the same as the executable, adding a reference to a dll will not include its config file and as far as I know dll's don't load config files on their own, rather they rely on the executable that reference them.

0
votes

Beyond not being able to do this, I would also advise against this approach.

Rather than trying to tighly couple your settings to the DLL library, consider more of a "Dependency Injection" type approach - i.e. where you pass in the value dependencies (i.e. settings) to the code you are calling (i.e. the library).

The advantage in this is you are not tied to a single method of storing settings. You can then use a database, different config file formats... even hard-coding. It also makes unit testing easier by removing the external file dependency.

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