Created
April 11, 2018 12:34
-
-
Save JamisonWhite/a889a3b190d8afd9ce0eebcec15592ce to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public static class AppSettingsHelper | |
{ | |
/// <summary> | |
/// Get the value or type default | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="name"></param> | |
/// <returns></returns> | |
public static T GetValue<T>(string name) | |
{ | |
return GetValue(name, default(T)); | |
} | |
/// <summary> | |
/// Get the value or default | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="name"></param> | |
/// <param name="defaultValue"></param> | |
/// <returns></returns> | |
public static T GetValue<T>(string name, T defaultValue) | |
{ | |
try | |
{ | |
var converter = TypeDescriptor.GetConverter(typeof(T)); | |
var value = ConfigurationManager.AppSettings[name]; | |
if (!string.IsNullOrEmpty(value)) | |
{ | |
return (T)converter.ConvertFromString(value); | |
} | |
} | |
catch (Exception) | |
{ | |
//NotSupportedException: don't care | |
//Other: Ignore | |
} | |
return defaultValue; | |
} | |
/// <summary> | |
/// Get the first key that exists | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="names"></param> | |
/// <param name="defaultValue"></param> | |
/// <returns></returns> | |
public static T GetValue<T>(IEnumerable<string> names, T defaultValue) | |
{ | |
foreach (var name in names) | |
{ | |
if (ConfigurationManager.AppSettings.AllKeys.Contains(name, StringComparer.InvariantCultureIgnoreCase)) | |
{ | |
return GetValue(name, defaultValue); | |
} | |
} | |
return defaultValue; | |
} | |
/// <summary> | |
/// Get folder name and create if it doesn't exist. | |
/// </summary> | |
/// <param name="name"></param> | |
/// <param name="defaultValue"></param> | |
/// <returns></returns> | |
public static string GetDirectory(string name, string defaultValue) | |
{ | |
var directoryPath = ConfigurationManager.AppSettings[name] ?? defaultValue; | |
if (string.IsNullOrEmpty(directoryPath)) | |
return string.Empty; | |
try | |
{ | |
if (!Directory.Exists(directoryPath)) | |
{ | |
Directory.CreateDirectory(directoryPath); | |
} | |
} | |
catch (Exception) | |
{ | |
// Ignore the exception here. Wait for a file not exists later. | |
} | |
return directoryPath; | |
} | |
/// <summary> | |
/// Get file name and create if it doesn't exist. | |
/// </summary> | |
/// <param name="name"></param> | |
/// <param name="defaultValue"></param> | |
/// <returns></returns> | |
public static string GetFile(string name, string defaultValue) | |
{ | |
var filePath = ConfigurationManager.AppSettings[name] ?? defaultValue; | |
if (string.IsNullOrEmpty(filePath)) | |
return string.Empty; | |
try | |
{ | |
var directoryPath = Path.GetDirectoryName(filePath); | |
if (!string.IsNullOrEmpty(directoryPath) && !Directory.Exists(directoryPath)) | |
{ | |
Directory.CreateDirectory(directoryPath); | |
} | |
} | |
catch (Exception) | |
{ | |
// Ignore the exception here. Wait for a file not exists later. | |
} | |
return filePath; | |
} | |
/// <summary> | |
/// Get connection | |
/// </summary> | |
/// <param name="name"></param> | |
/// <param name="defaultValue"></param> | |
/// <returns></returns> | |
public static string GetConnection(string name, string defaultValue) | |
{ | |
return ConfigurationManager.ConnectionStrings[name] == null ? defaultValue : ConfigurationManager.ConnectionStrings[name].ConnectionString; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment