// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace E { public class IniFile { private Dictionary> ini; private readonly Regex re = new Regex("\\[(.+?)\\]\r\n(((.+?)=(.*?)\r\n)*)", RegexOptions.Compiled); public IniFile(string fnm) { ini = new Dictionary>(); using(StreamReader sr = new StreamReader(fnm, Encoding.Default)) { string content = sr.ReadToEnd(); foreach(Match m in re.Matches(content)) { string section = m.Groups[1].Value; CaptureCollection keys = m.Groups[4].Captures; CaptureCollection vals = m.Groups[5].Captures; for(int i = 0; i < keys.Count; i++) { if(!ini.ContainsKey(section)) ini[section] = new Dictionary(); ini[section][keys[i].Value] = vals[i].Value; } } } } public string this[string section, string key] { get { return ini[section][key]; } } } public class Program { public static void Main(string[] args) { IniFile f = new IniFile(@"C:\test.ini"); Console.WriteLine(f["ost", "kills"]); Console.WriteLine(f["ost", "deaths"]); } } }