We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Using web config file from a DLL

rodmjay
rodmjay asked
on
Medium Priority
353 Views
Last Modified: 2012-08-14
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?
Comment
Watch Question

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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Have a look at ConfigurationSettings.Appsettings and you can use it from anywhere provided your web.config is defined in your web application

Rana

Author

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?
The example code I have given solves your problem. I don't think there is a standard solution for this.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.