Skip to content

Retrieving Application Settings

March 7, 2013

Here is a safe, generic way to retrieve application settings.

public static T GetAppSetting<T>(string key)
{
   if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
   {
      return (T)System.Convert.ChangeType(ConfigurationManager.AppSettings[key], 
                                        typeof(T), CultureInfo.InvariantCulture);
   }
   else
   {
      return default(T);
   }
}

Usage

int serverPort = GetAppSetting<int>("server_port");
About these ads
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: