/// <summary> /// Saves the current settings to registry /// </summary> /// /// <param name="config">Configration to save</param> /// <returns>true, if save action was successful</returns> private static bool saveSettingsToRegistry(BackupSettings config) { bool bRet = true; try { Type type = config.GetType(); PropertyInfo[] properties = type.GetProperties(); RegistryKey appKey = Registry.CurrentUser.CreateSubKey(REG_PATH_SETTINGS); //iterate all properties of config object foreach (PropertyInfo property in properties) { bRet &= SavePropertyToRegistry(config, appKey, property); } appKey.Close(); } catch (System.Exception e) { MessageBox.Show("Error during saving settings to registry " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } return(bRet); }
private static void TransferRegistryEntryToConfig(BackupSettings config, RegistryKey appKey, String name) { RegistryValueKind typ; PropertyInfo pi; try { //checked whether property exists pi = config.GetType().GetProperty(name); if (pi != null) { typ = appKey.GetValueKind(name); if (typeof(String).IsAssignableFrom(pi.PropertyType)) { pi.SetValue(config, appKey.GetValue(name) as String, null); } else if (typeof(int).IsAssignableFrom(pi.PropertyType)) { pi.SetValue(config, appKey.GetValue(name) as int?, null); } else if (typeof(bool).IsAssignableFrom(pi.PropertyType)) { pi.SetValue(config, (appKey.GetValue(name) as String).Equals(bool.TrueString), null); } else if (typeof(DateTime).IsAssignableFrom(pi.PropertyType)) { //office culture may not be system culture var ci = CultureInfo.InstalledUICulture; pi.SetValue(config, DateTime.Parse(appKey.GetValue(name) as String, ci), null); } else if (typeof(StringCollection).IsAssignableFrom(pi.PropertyType)) { String[] sArr = appKey.GetValue(name) as String[]; StringCollection sc = new StringCollection(); sc.AddRange(sArr); pi.SetValue(config, sc, null); } else if (typeof(List <int>).IsAssignableFrom(pi.PropertyType)) { String s = appKey.GetValue(name) as String; List <int> l = new List <int>(); char[] sep = new char[] { ',' }; s.Split(sep, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(x => l.Add(Convert.ToInt32(x))); pi.SetValue(config, l, null); } } } catch (System.Exception e) { MessageBox.Show("Error during fetching settings " + name + ": " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }