Created
February 22, 2017 01:54
-
-
Save fredgdaley2/9f6ea3822a86bac3b0ddf694f88c520c to your computer and use it in GitHub Desktop.
Retrieve application settings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Credit goes to this blog post https://dotnettips.wordpress.com/2013/03/07/retrieving-application-settings/ | |
//USAGE: int serverPort = GetAppSetting<int>("server_port"); | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment