public string GetValue(string group, string item) { foreach (IniLine line in Contents) { if (line is IniLineKV) { IniLineKV ilkv = line as IniLineKV; if (ilkv.GroupName.ToLower() == group.ToLower()) { if (ilkv.ItemName.ToLower() == item.ToLower()) { return(ilkv.ItemValue); } } } } return(null); }
public void SetValue(string group, string item, string value) { bool groupExists = false; foreach (IniLine line in Contents) { if (line is IniLineKV) { IniLineKV ilkv = line as IniLineKV; if (ilkv.GroupName.ToLower() == group.ToLower()) { groupExists = true; if (ilkv.ItemName.ToLower() == item.ToLower()) { if (ilkv.ItemValue != value) { //found it! Dirty = true; ilkv.ItemValue = value; ilkv.dirty = true; } return; } } } } //we didn't find it, have to add it. //if the group doesn't exist, we can just append this to the end of the file if (!groupExists) { Dirty = true; var line1 = new IniLine(""); line1.dirty = true; Contents.Add(line1); //whitespace var line2 = new IniLineGroup("[" + group + "]", group); line2.dirty = true; Contents.Add(line2); var line3 = new IniLineKV(item + "=" + value, group); Contents.Add(line3); } else { //otherwise we have to do a little legwork //scan for the last line of the target group int l = 0; int ll = 0; bool g = false; for (; l < Contents.Count; l++) { if (!g) //still searching for the appropriate group { if (Contents[l] is IniLineGroup) { if ((Contents[l] as IniLineGroup).GroupName == group) { //found the group g = true; continue; } } } else //we have a group, seeking to the end of it { if (Contents[l] is IniLineKV) { if ((Contents[l] as IniLineKV).GroupName != group) { //ll is now our target index. break out! break; } ll = l + 1; } } } Dirty = true; var line = new IniLineKV(item + "=" + value, group); line.dirty = true; Contents.Insert(ll, line); } }