Skip to content

Instantly share code, notes, and snippets.

@angelobelchior
Created March 28, 2014 19:01
Show Gist options
  • Save angelobelchior/9840443 to your computer and use it in GitHub Desktop.
Save angelobelchior/9840443 to your computer and use it in GitHub Desktop.
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