Created
March 28, 2014 19:01
-
-
Save angelobelchior/9840443 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Configuration; | |
using System.Linq; | |
namespace Configuration | |
{ | |
public static class AppSetting | |
{ | |
public static bool GetBoolean(string key, bool defaultValue = false) | |
{ | |
var value = GetValue(key); | |
bool.TryParse(value, out defaultValue); | |
return defaultValue; | |
} | |
public static int GetInt32(string key, int defaultValue = 0) | |
{ | |
var value = GetValue(key); | |
int.TryParse(value, out defaultValue); | |
return defaultValue; | |
} | |
public static string GetString(string key, string defaultValue = "") | |
{ | |
var value = GetValue(key); | |
if (!string.IsNullOrEmpty(value)) | |
return value; | |
return defaultValue; | |
} | |
public static DateTime? GetDateTime(string key, DateTime? defaultValue = null) | |
{ | |
var value = GetValue(key); | |
DateTime dateTime; | |
if (DateTime.TryParse(value, out dateTime)) | |
return dateTime; | |
return defaultValue; | |
} | |
private static string GetValue(string key) | |
{ | |
if (ConfigurationManager.AppSettings.AllKeys.Contains(key)) | |
return ConfigurationManager.AppSettings[key]; | |
return string.Empty; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment