namespace Fastasp.SysConfig
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class SystemConfig
{
private static bool m_gotSettings = false;
private static Hashtable m_configCache;
private const string m_buildNumberKey = "build_number_key";
public SystemConfig()
{
if (!m_gotSettings)
{
readConfig();
m_gotSettings = true;
}
}
public bool getBool(string section, string key)
{
return bool.Parse(getConfigString(section, key));
}
public int getInt(string section, string key)
{
return int.Parse(getConfigString(section, key));
}
public long getLong(string section, string key)
{
return long.Parse(getConfigString(section, key));
}
public decimal getDecimal(string section, string key)
{
return decimal.Parse(getConfigString(section, key));
}
public float getFloat(string section, string key)
{
return float.Parse(getConfigString(section, key));
}
public string getString(string section, string key)
{
return getConfigString(section, key);
}
public string getBuildNumber()
{
return m_configCache[m_buildNumberKey].ToString();
}
private string getConfigString(string section, string key)
{
try
{
string ConfigSetting = m_configCache[getTrueKey(section,key)].ToString();
if (ConfigSetting == null)
throw new ApplicationException ("Failed to read config for section=" + section + " and key=" + key);
return ConfigSetting;
}
catch (Exception ex)
{
throw new Exception("Failed to read config for section=" + section + " and key=" + key, ex);
}
}
private void readConfig()
{
string currentAssembly;
string configFileName = "SystemConfig.xml";
string configFullpath = null;
bool cacheConfigSettings = false;
try
{
currentAssembly = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
m_configCache = new Hashtable();
XmlDocument configXML = new XmlDocument();
string section, key, trueKey;
//configFullpath = Path.GetDirectoryName(currentAssembly) + "/../../Config/" + configFileName;
configFullpath = "/asp.net/Config/" + configFileName;
configXML.Load(configFullpath);
//Check the cache setting
cacheConfigSettings = Boolean.Parse(configXML.DocumentElement.GetAttribute("cacheSettings"));
foreach ( XmlNode sectionNode in configXML.DocumentElement.ChildNodes)
{
section = sectionNode.Name;
foreach ( XmlNode keyNode in sectionNode)
{
key = keyNode.Name;
trueKey = getTrueKey(section, key);
if (m_configCache.ContainsKey(trueKey))
m_configCache[trueKey] = keyNode.InnerText;
else
m_configCache.Add(trueKey, keyNode.InnerText);
}
}