Quantcast
Channel: Reading settings from app.config or web.config in .NET - Stack Overflow
Viewing all articles
Browse latest Browse all 29

Answer by Ashwini Jindal for Reading settings from app.config or web.config in .NET

$
0
0

As I found the best approach to access application settings variables in a systematic way by making a wrapper class over System.Configuration as below

public class BaseConfiguration{    protected static object GetAppSetting(Type expectedType, string key)    {        string value = ConfigurationManager.AppSettings.Get(key);        try        {            if (expectedType == typeof(int))                return int.Parse(value);            if (expectedType == typeof(string))                return value;            throw new Exception("Type not supported.");        }        catch (Exception ex)        {            throw new Exception(string.Format("Config key:{0} was expected to be of type {1} but was not.",                key, expectedType), ex);        }    }}

Now we can access needed settings variables by hard coded names using another class as below:

public class ConfigurationSettings:BaseConfiguration{    #region App setting    public static string ApplicationName    {        get { return (string)GetAppSetting(typeof(string), "ApplicationName"); }    }    public static string MailBccAddress    {        get { return (string)GetAppSetting(typeof(string), "MailBccAddress"); }    }    public static string DefaultConnection    {        get { return (string)GetAppSetting(typeof(string), "DefaultConnection"); }    }    #endregion App setting    #region global setting    #endregion global setting}

Viewing all articles
Browse latest Browse all 29

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>