using System;
using System.Xml;
using System.Web.UI;
using System.IO;
using System.Web;
using System.Collections;
namespace MyApp.Utility
{
public class GetFromWebConfig
{
protected static Hashtable cachedValues = new Hashtable();
protected static string fileName;
static GetFromWebConfig()
{
fileName = "c:\directorypath\web.config";
\\substitute "directorypath" with the locacation of your
\\web.config file
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNode oNode = doc.DocumentElement.SelectSingleNode("appSettings");
for (int i = 0;i < oNode.ChildNodes.Count;i++)
{
string key = oNode.ChildNodes.Item(i).Attributes.Item(0).Value;
string data = oNode.ChildNodes.Item(i).Attributes.Item(1).Value;
cachedValues.Add(key, data);
}
}
public static string getFromAppSettings(string KeyName)
{
object result = cachedValues[KeyName];
if(result == null)
{
return String.Empty;
}
return result.ToString();
}
}
}
Assuming your web.config looks something like this
<appSettings>
<add key="MyWebsiteRootPath" value="C:\MyWebSite"/>
<add key="UploadDir" value="\MyWebSite\Upload\"/>
<add key="ImageDirectory" value="MyWebSite\Images\"/>
</appSettings>
GetFromWebConfig.getFromAppSettings("ImageDirectory") will return "MyWebSite\Images\"/>