Created
November 6, 2014 00:49
-
-
Save Suchiman/6481a0fea0da793bc448 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
| using System; | |
| using System.Configuration; | |
| using System.Linq; | |
| namespace Some.Namespace | |
| { | |
| public static class ConfigHelper | |
| { | |
| private static Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); | |
| public static bool KeyExists(string key) | |
| { | |
| return conf.AppSettings.Settings.AllKeys.Contains(key); | |
| } | |
| public static void Remove(string key) | |
| { | |
| if (KeyExists(key)) | |
| { | |
| conf.AppSettings.Settings.Remove(key); | |
| conf.Save(); | |
| } | |
| } | |
| public static void SetValue(string key, string value) | |
| { | |
| if (KeyExists(key)) | |
| { | |
| conf.AppSettings.Settings.Remove(key); | |
| } | |
| conf.AppSettings.Settings.Add(key, value); | |
| conf.Save(); | |
| } | |
| public static string GetString(string key) | |
| { | |
| KeyValueConfigurationElement entry = conf.AppSettings.Settings[key]; | |
| if (entry == null) | |
| { | |
| throw new ConfigurationErrorsException(); | |
| } | |
| return entry.Value; | |
| } | |
| public static string GetString(string key, string @default) | |
| { | |
| if (!KeyExists(key)) | |
| { | |
| SetValue(key, @default); | |
| } | |
| return GetString(key); | |
| } | |
| public static int GetInt(string key) | |
| { | |
| string value = GetString(key); | |
| return Convert.ToInt32(value); | |
| } | |
| public static int GetInt(string key, int @default) | |
| { | |
| if (!KeyExists(key)) | |
| { | |
| SetValue(key, @default.ToString()); | |
| } | |
| return GetInt(key); | |
| } | |
| public static bool GetBoolean(string key) | |
| { | |
| string value = GetString(key); | |
| return Convert.ToBoolean(value); | |
| } | |
| public static bool GetBoolean(string key, bool @default) | |
| { | |
| if (!KeyExists(key)) | |
| { | |
| SetValue(key, @default.ToString()); | |
| } | |
| return GetBoolean(key); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment