/// <summary> /// Adds or sets the value of the property. /// </summary> /// <param name="section">The section of the property.</param> /// <param name="key">The key of the property.</param> /// <param name="value">The property value.</param> public void Put(string section, string key, string value) { IniSection sect = null; if (p_Sections.Contains(section)) { sect = this.GetSection(section); } else { if (p_Sections.Count > 0) { IniSection lastSect = (IniSection)p_Sections[p_Sections.Count - 1]; // Insert empy line? if (!string.IsNullOrEmpty(lastSect[lastSect.Count - 1].Value)) { lastSect.Add(new IniProperty(section, IniType.EmptyLine, "")); } } sect = new IniSection(section); p_Sections.Add(section, sect); } if (sect.Contains(key)) { sect.Get(key).Value = value; } else { sect.Add(new IniProperty(section, key, value)); } }
/// <summary> /// Loads the properties from the configuration file. /// </summary> public void Load() { if (!File.Exists(this.Filename)) { return; } this.p_Sections.Clear(); using (var reader = new StreamReader(this.Filename)) { string key = string.Empty; string line; int lineCount = 0; IniSection section = this.GetSection(SectionHeader); while ((line = reader.ReadLine()) != null) { lineCount++; line = line.Trim(); // Empty line. if (string.IsNullOrEmpty(line)) { key = IniType.EmptyLine.ToString() + lineCount; // Use key above if available, otherwise generate random if (section.Contains(key)) { section.Add(new IniProperty(section.Name, IniType.EmptyLine, line)); } else { section.Add(new IniProperty(section.Name, IniType.EmptyLine, key, line)); } } else { char firstChar = char.Parse(line.Substring(0, 1)); switch (firstChar) { case ';': case '#': // Comment line key = IniType.Comment.ToString() + lineCount; // Use key above if available, otherwise generate random if (section.Contains(key)) { section.Add(new IniProperty(section.Name, IniType.Comment, line)); } else { section.Add(new IniProperty(section.Name, IniType.Comment, key, line)); } break; case '[': // Section line if (!line.EndsWith("]")) { // Line doesn't end with a closing bracket. goto default; } else { int start = line.IndexOf('[') + 1; int length = line.IndexOf(']') - line.IndexOf('[') - 1; string name = line.Substring(start, length); if (p_Sections.Contains(name)) { section = (IniSection)p_Sections[name]; } else { section = new IniSection(name); p_Sections.Add(name, section); } } break; default: // Valid property line if (line.Contains("=") && section.Name != SectionHeader) { string[] split = line.Split('='); string value = split[1]; key = split[0]; if (section.Contains(split[0])) { throw new Exception(string.Format("Section '{0}' already contains property '{1}'. Value: {2}", section.Name, key, value)); } section.Add(new IniProperty(section.Name, key, value)); } else // Invalid line { key = IniType.Invalid.ToString() + lineCount; // Use key above if available, otherwise generate random if (section.Contains(key)) { section.Add(new IniProperty(section.Name, IniType.Invalid, line)); } else { section.Add(new IniProperty(section.Name, IniType.Invalid, key, line)); } } break; } } } } }