views:

479

answers:

3

I need to access a web application's web.config from a referenced assembly. I need to get the file's path or the configuration object. I can't do this using System.Reflection.Assembly.GetEntryAssembly as I do for the application's configuration of a windows exe.

Thanks

+1  A: 

You can use the ConfigurationManager class from the System.Configuration assembly.

But you will not get the path, because the configuration is a mix of a lot of configuration files (machin.config, several web.config etc)

Think Before Coding
Thanks, the solution is right.
Marco Bettiolo
+4  A: 

I'm using the following code in my class libraries to read from the config file:

using System.Configuration;

...

string value = ConfigurationManager.AppSettings.Get("myKey");
// returns null if the specified key does not exist

This works to read the app settings in both web applications (web.config) and windows forms/console applications (application.exe.config).

M4N
Thanks, your solution works too, but Think Before Coding was first to point me in the right direction! I would add to both the accepted answer thick but it is not possible.
Marco Bettiolo
A: 

That is not good practice, as it couples too many things together.

Instead, pass the required configuration settings in from the host assembly to any assemblies which it references.

In this way, the referenced assemblies can be built and tested without relying on configuration files.

Justice