views:

356

answers:

4

I have a console application project and library project (dll) in one solution. The library project has app.config file where I store a connection string to database. The console application references this dll.

When I compile the console application and deploy it with the library I am not able to access the app.config file that belongs the dll when I need to change the connection string after the application is deployed.

This are the files that I see, but not .config file:

library.dll library.pdb console.application console.exe console.exe.manifest console.pdb console.vshost.application console.vshest.exe

Where did I go wrong?

A: 

Check the "Copy to the Output Directory" property for your config file.

jonaspp
The "Build Action" property may help too.
jonaspp
DLLs don't have separate app.config files. See David's answer.
jonsb
+7  A: 

By default, there is only 1 ".config" file for a running application. It is the ".config" file associated with the EXE that started the program. You should probably copy the config values from the DLL's config file into the console app's config file. If you really want to keep them separate then you can't use the default ConfigurationSettings.AppSettings dictionary. See this question for more info.

David
Thanks for your answer. I copied the content of the app.config from the dll to the host, and I was still able to control the behaviour of the dll from the host app.config via applicationSettings. (Thanks to others who responded to my inquiry, with the solution.)
+1  A: 

Do you create a app.config file for your exe ( not just for your dll)? You have to, and make sure that you copy whatever setting you have in your dll config to your exe config.

Or you can use "Add as Link" to link the app.config to your exe.

Ngu Soon Hui
+1  A: 

By default, each process will use it's own configuration file. If you want the console application to have a configuration file you will need to add that to your project. After adding the App.config to your project whenever your project is built the App.config will be copied to the output folder as <application>.exe.config where <application> is your application name (e.g. ConsoleApplication1.exe.config). (Web.config is more complicated.)

Typically, configuration is then added to this application configuration file.

So the easiest way to configure your library assembly is to add its specific configuration to the hosting application's configuration file.

Now that can be a bit ugly. One way to make it less ugly is to have the application configuration file simply reference your config file using the ConfigSource attribute. That way you can deploy your assembly along with your config file and simply have the hosting application add a few lines to their config file to reference your config. First they have to add a reference to the configSections:

  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </configSections>

Next they need to add a reference to your config file:

<loggingConfiguration configSource="MyLoggingConfig.xml"/>

Now, maybe you don't want the users of your assembly to even know there is a configuration file. If that is the case, you could create your own standalone configuration file and open it up using ConfigurationManager.OpenMappedExeConfiguration. Here is another example on how to use OpenMappedExeConfiguration.

Tuzo