/// <summary>Gets a section by name.</summary> /// <param name="sectionName">The name of the section</param> /// <param name="createWhenNotFound">If the section was not found, create a new section with that name and return that.</param> /// <returns>The retrieved or new IniSection object with that name.</returns> protected IniSection getSection(String sectionName, Boolean createWhenNotFound) { IniSection iniSection = null; String sectionLower = sectionName.ToLowerInvariant(); foreach (IniSection section in this.iniSections) { if (section.getName().ToLowerInvariant().Equals(sectionLower)) { iniSection = section; break; } } if (iniSection == null && createWhenNotFound) // doesn't exist yet { iniSection = new IniSection(sectionName); this.iniSections.Add(iniSection); this.removedSections.Remove(sectionLower); } return(iniSection); }
/// <summary>Reads the ini contents of a stream, and returns it as a list of ini sections.</summary> /// <param name="stream>the InputStream to read the ini data from.</param> /// <returns>A List of IniSection objects with the read data.</returns> protected void readIniContents(StreamReader stream) { this.iniSections = new List<IniSection>(); this.removedSections = new List<String>(); if (stream == null) return; try { String input = null; IniSection iniSection = null; String[] keyValue; List<String> initext = readLinesFromTextStream(stream); for (int i = 0; i < initext.Count; i++) { input = initext[i]; if (input.StartsWith("[") && input.Contains("]")) { String sectionName = input.Substring(1, input.IndexOf("]")-1); if (!sectionName.Contains("[")) // valid ini section { iniSection = null; int sectionIndex = -1; for (int j = 0; j < this.iniSections.Count; j++) { IniSection testsec = this.iniSections[j]; if (testsec.getName().Equals(sectionName)) sectionIndex = j; } if (sectionIndex > -1) { iniSection = this.iniSections[sectionIndex]; } if (iniSection == null) // doesn't exist yet { iniSection = new IniSection(sectionName); this.iniSections.Add(iniSection); } else // section already exists; don't allow merging of different same-name sections. (needed for correct deleting of extra ini entries) iniSection = null; } } else if (iniSection!=null) // ini section was found (everything before first ini section is ignored) { keyValue = getKeyAndValue(input); if (keyValue!=null && keyValue.Length == 2) iniSection.setStringValue(keyValue[0], keyValue[1]); } } } catch { } }
/// <summary>Gets a section by name.</summary> /// <param name="sectionName">The name of the section</param> /// <param name="createWhenNotFound">If the section was not found, create a new section with that name and return that.</param> /// <returns>The retrieved or new IniSection object with that name.</returns> protected IniSection getSection(String sectionName, Boolean createWhenNotFound) { IniSection iniSection = null; String sectionLower = sectionName.ToLowerInvariant(); foreach (IniSection section in this.iniSections) if (section.getName().ToLowerInvariant().Equals(sectionLower)) { iniSection = section; break; } if (iniSection == null && createWhenNotFound) // doesn't exist yet { iniSection = new IniSection(sectionName); this.iniSections.Add(iniSection); this.removedSections.Remove(sectionLower); } return iniSection; }
/// <summary>Sets a Boolean value in the ini file, in the chosen boolean save mode. /// This action does not save the file.</summary> /// <param name="sectionName">The name of the section the key should be in</param> /// <param name="key">The name of the key</param> /// <param name="value">Value to write</param> /// <param name="booleanmode">The BooleanMode (True/False, Yes/No, 1/0, etc) to use for saving Booleans as String.</param> /// <param name="removeComments">True to remove any comments put behind the value. The default behaviour is to filter out the comment and paste it behind the new value.</param> public void setBoolValue(String sectionName, String key, Boolean value, BooleanMode booleanmode, Boolean removeComments) { IniSection iniSection = getSection(sectionName, true); iniSection.setBoolValue(key, value, booleanmode, removeComments); }
/// <summary>Sets a Character value in the ini file. This action does not save the file.</summary> /// <param name="sectionName">The name of the section the key should be in</param> /// <param name="key">The name of the key</param> /// <param name="value">Value to write</param> /// <param name="removeComments">True to remove any comments put behind the value. The default behaviour is to filter out the comment and paste it behind the new value.</param> public void setCharValue(String sectionName, String key, char value, Boolean removeComments) { IniSection iniSection = getSection(sectionName, true); iniSection.setCharValue(key, value, removeComments); }
/// <summary>Sets a String value in the ini file. This action does not save the file.</summary> /// <param name="sectionName">The name of the section the key should be in</param> /// <param name="key">The name of the key</param> /// <param name="value">Value to write</param> public void setStringValue(String sectionName, String key, String value) { IniSection iniSection = getSection(sectionName, true); iniSection.setStringValue(key, value); }
/// <summary>Reads the ini contents of a stream, and returns it as a list of ini sections.</summary> /// <param name="stream>the InputStream to read the ini data from.</param> /// <returns>A List of IniSection objects with the read data.</returns> protected void readIniContents(StreamReader stream) { this.iniSections = new List <IniSection>(); this.removedSections = new List <String>(); if (stream == null) { return; } try { String input = null; IniSection iniSection = null; String[] keyValue; List <String> initext = readLinesFromTextStream(stream); for (int i = 0; i < initext.Count; i++) { input = initext[i]; if (input.StartsWith("[") && input.Contains("]")) { String sectionName = input.Substring(1, input.IndexOf("]") - 1); if (!sectionName.Contains("[")) // valid ini section { iniSection = null; int sectionIndex = -1; for (int j = 0; j < this.iniSections.Count; j++) { IniSection testsec = this.iniSections[j]; if (testsec.getName().Equals(sectionName)) { sectionIndex = j; } } if (sectionIndex > -1) { iniSection = this.iniSections[sectionIndex]; } if (iniSection == null) // doesn't exist yet { iniSection = new IniSection(sectionName); this.iniSections.Add(iniSection); } else // section already exists; don't allow merging of different same-name sections. (needed for correct deleting of extra ini entries) { iniSection = null; } } } else if (iniSection != null) // ini section was found (everything before first ini section is ignored) { keyValue = getKeyAndValue(input); if (keyValue != null && keyValue.Length == 2) { iniSection.setStringValue(keyValue[0], keyValue[1]); } } } } catch { } }