public void Save() { RegistryKey key = Registry.CurrentUser.CreateSubKey(_strKey); if (key != null) { foreach (KeyValuePair <string, object> de in Options) { TypeConverter tc; String s; if (de.Value == null) { s = ""; } else { if (de.Value is int[]) { tc = new IntegerArrayConverter(); } else { tc = TypeDescriptor.GetConverter(de.Value); } s = tc.ConvertToString(de.Value); } key.SetValue(de.Key, s); } key.Flush(); } }
public T Get <T>(String optionName, Object defaultValue) { Debug.Assert(!String.IsNullOrEmpty(optionName)); object value; if (Options.TryGetValue(optionName, out value)) { return((T)value); } else { String s = (String)Registry.GetValue("HKEY_CURRENT_USER\\" + _strKey, optionName, null); if (s == null) { return((T)defaultValue); } else { TypeConverter tc; if (typeof(T) == typeof(int[])) { tc = new IntegerArrayConverter(); } else { tc = TypeDescriptor.GetConverter(typeof(T)); } try { value = tc.ConvertFromString(s); } catch (Exception) { value = defaultValue; } Options.Add(optionName, value); return((T)value); } } }