private void Load(TextReader reader) { IniSection section = null; string line; while ((line = reader.ReadLine()) != null) { line = line.Trim(); // skip empty lines if (line == string.Empty) continue; // skip comments if (line.StartsWith(";") || line.StartsWith("#")) continue; if (line.StartsWith("[") && line.EndsWith("]")) { var sectionName = line.Substring(1, line.Length - 2); if (!_sections.ContainsKey(sectionName)) { section = new IniSection(sectionName); _sections.Add(sectionName, section); } continue; } if (section != null) { var keyValue = line.Split(new[] { "=" }, 2, StringSplitOptions.RemoveEmptyEntries); if (keyValue.Length != 2) continue; section.Set(keyValue[0].Trim(), keyValue[1].Trim()); } } }
/// <summary> /// Get a section by name. If the section doesn't exist, it is created. /// </summary> /// <param name="sectionName">The name of the section.</param> /// <returns>A section. If the section doesn't exist, it is created.</returns> public IniSection Section(string sectionName) { IniSection section; if (!_sections.TryGetValue(sectionName, out section)) { section = new IniSection(sectionName); _sections.Add(sectionName, section); } return section; }
private void Load(TextReader reader) { IniSection section = null; string line; while ((line = reader.ReadLine()) != null) { line = line.Trim(); // skip empty lines if (line == string.Empty) continue; // skip comments //if (line.StartsWith(";") || line.StartsWith("#")) // continue; if (line.StartsWith("[") && line.EndsWith("]")) { var sectionName = line.Substring(1, line.Length - 2); if (!_sections.ContainsKey(sectionName)) { section = new IniSection(sectionName); _sections.Add(sectionName, section); } continue; } if (section != null) { string patt = @"^;?(?<key>[^\n\s]+)\s*=\s?(?<value>[^\n]+)?$"; Match match = Regex.Match(line, patt, RegexOptions.IgnoreCase | RegexOptions.Multiline); /* var keyValue = line.Split(new[] { "=" }, 2, StringSplitOptions.RemoveEmptyEntries); if (keyValue.Length != 2) continue;*/ if (match.Success) { //var g = match.Groups; // Finally, we get the Group value and display it. string key = match.Groups["key"].Value; string value = match.Groups["value"].Value; Console.WriteLine("{0} = {1}", key, value); //Console.WriteLine(match.Groups[1].Value); } } } }