void setComment(IniFileElement el, string comment) { int index = parent.elements.IndexOf(el); if (IniFileSettings.CommentChars.Length == 0) throw new NotSupportedException("Comments are currently disabled. Setup ConfigFileSettings.CommentChars property to enable them."); IniFileCommentary com; if (index > 0 && parent.elements[index - 1] is IniFileCommentary) { com = ((IniFileCommentary)parent.elements[index - 1]); if (comment == "") parent.elements.Remove(com); else { com.Comment = comment; com.Intendation = el.Intendation; } } else if (comment != "") { com = IniFileCommentary.FromComment(comment); com.Intendation = el.Intendation; parent.elements.Insert(index, com); } }
string getComment(IniFileElement el) { int index = parent.elements.IndexOf(el); if (index != 0 && parent.elements[index - 1] is IniFileCommentary) return ((IniFileCommentary)parent.elements[index - 1]).Comment; else return ""; }
/// <summary>Returns a list of IniFileElement object in the currect section. The first element of /// returned collection will be a IniFileSectionStart.</summary> /// <exception cref="System.InvalidOperationException">A stream is not currently at the IniFileSectionStart.</exception> public List<IniFileElement> ReadSection() { if (current == null || !(current is IniFileSectionStart)) throw new InvalidOperationException("The current position of the reader must be at IniFileSectionStart. Use GotoSection method"); List<IniFileElement> ret = new List<IniFileElement>(); IniFileElement theCurrent = current; ret.Add(theCurrent); string text = "", temp; while ((temp = base.ReadLine()) != null) { if (IniFileSectionStart.IsLineValid(temp.Trim())) { current = new IniFileSectionStart(temp); break; } text += temp + Environment.NewLine; } if (text.EndsWith(Environment.NewLine) && text != Environment.NewLine) text = text.Substring(0, text.Length - Environment.NewLine.Length); ret.AddRange(ParseText(text)); return ret; }
/// <summary>Reads and parses next line from the config file.</summary> /// <returns>Created ConfigFileElement.</returns> public IniFileElement ReadElement() { current = ParseLine(base.ReadLine()); return current; }
/// <summary>Seeks to the section of specified name. If such section is not found, /// the function returns NULL and leaves the stream at the end of file.</summary> /// <param name="sectionName">Name of section to find.</param> public IniFileSectionStart GotoSection(string sectionName) { IniFileSectionStart sect = null; string str; while (true) { str = ReadLine(); if (str == null) { current = null; return null; } if (IniFileSectionStart.IsLineValid(str)) { sect = ParseLine(str) as IniFileSectionStart; if (sect != null && (sect.SectionName == sectionName || (!IniFileSettings.CaseSensitive && sect.SectionName.ToLowerInvariant() == sectionName))) { current = sect; return sect; } } } }
/// <summary>Writes INI file element to the file.</summary> /// <param name="element">Element to write.</param> public void WriteElement(IniFileElement element) { if (!IniFileSettings.PreserveFormatting) element.FormatDefault(); // do not write if: if (!( // 1) element is a blank line AND blank lines are not allowed (element is IniFileBlankLine && !IniFileSettings.AllowBlankLines) // 2) element is an empty value AND empty values are not allowed || (!IniFileSettings.AllowEmptyValues && element is IniFileValue && ((IniFileValue)element).Value == ""))) base.WriteLine(element.Line); }