Using web config file from a DLL

I am creating a DLL library alongside my asp.net application.  I would like to use information from the Web.config file, how do you do that?
LVL 8
rodmjayAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

existenz2Commented:
Have a look at: http://www.dotnet247.com/247reference/msgs/27/137570.aspx

A solution is given there by Johnny Random:

using System;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;

namespace Examples
{
/// <summary>
/// Used to access configuration information for the application
/// </summary>
public class AppConfig
{

private static bool firstUse = true;
private static NameValueCollection properties = null;

/// <summary>
/// Get a value by the key name
/// </summary>
/// <param name="Key">Name of the Key</param>
/// <returns>The configuration setting</returns>
public static string Properties(string Key)
{
return Properties(Key,false);
}
/// <summary>
/// Get a value by the key name
/// </summary>
/// <param name="Key">Name of the Key</param>
/// <param name="ForceRead">Forces the value to be re-read</param>
/// <returns>The configuration setting</returns>
public static string Properties(string Key, bool ForceRead)
{
ReadConfig(ForceRead);
return properties.Get(Key);
}

/// <summary>
/// Get a value by the key index number
/// </summary>
/// <param name="KeyIndex">Number of the Key</param>
/// <returns>The configuration setting</returns>
public static string Properties(int KeyIndex)
{
return Properties(KeyIndex,false);
}

/// <summary>
/// Get a value by the key index number
/// </summary>
/// <param name="KeyIndex">Number of the Key</param>
/// <param name="ForceRead">Forces the value to be re-read</param>
/// <returns>The configuration setting</returns>
public static string Properties(int KeyIndex, bool ForceRead)
{
ReadConfig(ForceRead);
return properties.Get(KeyIndex);
}

/// <summary>
/// Reads the configuration information if it hasn't been read already
/// </summary>
public static void ReadConfig()
{
ReadConfig(false);
}
/// <summary>
/// Reads the configuration information
/// </summary>
/// <param name="ForceRead">If true will force a re-read of the config
info, if false only read the config info if it hasn't been read
already.</param>
public static void ReadConfig(bool ForceRead)
{
// Read config file settings
if (firstUse || ForceRead)
{

//properties = System.Configuration.ConfigurationSettings.AppSettings;

//
XmlTextReader xmlTR = new XmlTextReader("YourConfigFile.config");
xmlTR.WhitespaceHandling = WhitespaceHandling.None;
properties = new NameValueCollection();
try
{

while(xmlTR.Read())
{

switch (xmlTR.NodeType)
{
case XmlNodeType.Element:
//Console.Write("<{0}>", xmlTR.Name);
if (xmlTR.HasAttributes && xmlTR.Name == "add")
{

properties.Add(xmlTR.GetAttribute("key"),xmlTR.GetAttribute("value"));
Console.WriteLine( xmlTR.GetAttribute("key") + " = " +
xmlTR.GetAttribute("value"));
}
break;
case XmlNodeType.Text:
//Console.Write(xmlTR.Value);
break;
case XmlNodeType.CDATA:
//Console.Write("<![CDATA[{0}]]>", xmlTR.Value);
break;
case XmlNodeType.ProcessingInstruction:
//Console.Write("<?{0} {1}?>", xmlTR.Name, xmlTR.Value);
break;
case XmlNodeType.Comment:
//Console.Write("<!--{0}-->", xmlTR.Value);
break;
case XmlNodeType.XmlDeclaration:
//Console.Write("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
//Console.Write("<!DOCTYPE {0} [{1}]", xmlTR.Name, xmlTR.Value);
break;
case XmlNodeType.EntityReference:
//Console.Write(xmlTR.Name);
break;
case XmlNodeType.EndElement:
//Console.Write("</{0}>\n", xmlTR.Name);
break;
}

}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
firstUse = false;
}
}

}
}

You have to copy your config file to the windows\system32 or winnt\system32
directory.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
irps20001Commented:
Have a look at ConfigurationSettings.Appsettings and you can use it from anywhere provided your web.config is defined in your web application

Rana
rodmjayAuthor Commented:
I do not believe that you can use appsettings from a DLL, since it is not running in the same application.  I am looking for cleaner way to do this and i believe there is a way to do this with the newest version of asp.net.  Does anyone know to do this?
existenz2Commented:
The example code I have given solves your problem. I don't think there is a standard solution for this.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.