?
Solved

Using web config file from a DLL

Posted on 2006-04-06
6
Medium Priority
?
338 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?
0
Comment
Question by:rodmjay
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
existenz2 earned 1000 total points
ID: 16389776
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
 
LVL 7

Expert Comment

by:irps20001
ID: 16389932
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
 
LVL 8

Author Comment

by:rodmjay
ID: 16427517
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
 
LVL 14

Expert Comment

by:existenz2
ID: 16434056
The example code I have given solves your problem. I don't think there is a standard solution for this.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question