Search code examples
c#asp.netweb-configprojects-and-solutions

Reference Web.Config file from another project in same solution C#


I have a VC2010 C# Solution, with a number of projects in it.

So for example, I have a web project, and I have a class library.

In the web.config file, I have a key in the <appSettings> section, e.g.

<add key="FileDirectory" value="G:\ftproot\sales" />

I have also added a key in the Web.Production.config file to reflect the file directory on the server.

So when I reference it in my web project (It's MVC) - I do so like this:

var FTPPath = ConfigurationManager.AppSettings["FileDirectory"];

this works fine within my web project. However, I also need to reference this in the class library, which gets to my question - Is there a way to reference a key in the web.config file from another project, e.g. a class library, in the same solution??

All help is appreciated. Thanks


Solution

  • Yes you can use exactly the same code. .Net will look up the configuration key in the config file of the application which started the app domain. A class library used by such an application will have access to it's config file.