/// <summary> /// set a group setting /// </summary> public void SetSettingGroup(SettingGroup group) { string id = group.Id; if (GroupsDictionary.ContainsKey(id)) { GroupsDictionary[id] = group; } else { GroupsDictionary.Add(id, group); } bool oldAutoSave = AutoSave; AutoSave = false; foreach (var item in group.Settings) { SetSetting(id + "." + item.Key, item.Value); } AutoSave = oldAutoSave; if (AutoSave) { SaveSettings(); } }
/// <summary> /// set a setting value for a group /// </summary> public void SetGroupSetting(string groupId, string name, object value) { if (!string.IsNullOrEmpty(groupId)) { SettingGroup group; if (!GroupsDictionary.TryGetValue(groupId, out group)) { group = new SettingGroup { Id = groupId }; GroupsDictionary.Add(groupId, group); } group.SetSetting(name, value); } }
/// <summary> /// try get a setting group by ID /// </summary> /// <param name="id">the ID of the setting group</param> /// <param name="groupSetting">the out setting group</param> /// <returns>returns true if exists</returns> public bool TryGetSettingGroup(string id, out SettingGroup settingGroup) { return(GroupsDictionary.TryGetValue(id, out settingGroup)); }
/// <summary> /// Load the settings from local file /// </summary> public bool LoadSettings() { bool oldAutoSave = AutoSave; AutoSave = false; try { if (string.IsNullOrEmpty(SettingsFile)) { return(false); } using (TextReader reader = File.OpenText(SettingsFile)) { string currentGroupId = string.Empty; SettingGroup currentGroup = null; string buffer; while ((buffer = reader.ReadLine()) != null) { buffer = buffer.Trim(); if (string.IsNullOrEmpty(buffer) || buffer.StartsWith("#")) { continue; } string[] parts = buffer.Split(SettingValueDelimiter); if (parts.Length > 0) { string key = parts[0].Trim(); string value = null; if (parts.Length > 1) { value = parts[1].Trim(); } if (string.Equals("group", key, StringComparison.OrdinalIgnoreCase)) { // assign a GUID as group ID if (string.IsNullOrEmpty(value)) { value = Guid.NewGuid().ToString(); } currentGroupId = value; currentGroup = new SettingGroup() { Id = value }; GroupsDictionary.Add(value, currentGroup); // adding ID to both dictionaries currentGroup.SetSetting("Id", value); SetSetting(currentGroupId + ".Id", value); } else if (string.Equals("endgroup", key, StringComparison.OrdinalIgnoreCase)) { currentGroupId = null; currentGroup = null; } else { if (string.IsNullOrEmpty(currentGroupId)) { SetSetting(key, value); } else { string fullName = currentGroupId + "." + key; SetSetting(fullName, value); currentGroup.SetSetting(key, value); } } } } reader.Close(); } return(true); } catch (Exception err) { // todo } AutoSave = oldAutoSave; return(false); }