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?
 
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.
0
 
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
0
 
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?
0
 
existenz2Commented:
The example code I have given solves your problem. I don't think there is a standard solution for this.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.