public static INIFile Read(string file) { string [] rawLines = new string[0]; try { rawLines = File.ReadAllLines(file); } catch { } var lines = new List<INILine>(); string currentSection = ""; foreach (var rawLine in rawLines) { var line = rawLine.TrimEnd(); if (line.Length == 0) { // empty line lines.Add(new INILine(null, null, null, null)); continue; } string comment = null; if (line[0] == ';') // ; asdasd { lines.Add(new INILine(null, null, null, line.Substring(1))); continue; } if (line.Contains(';')) // comment at the end? { var parts = line.Split(new[] { ';' }, 2); line = parts[0].Trim(); comment = parts[1]; } if (line[0] == '[') // [asd] or [asd] ; foo { currentSection = line.Substring(1, line.Length - 2); lines.Add(new INILine(currentSection, null, null, comment)); } else // key=value or key=value ; foo { var parts = line.Split(new[] { '=' }, 2); if (parts.Length != 2) continue; lines.Add(new INILine(currentSection, parts[0], parts[1], comment)); } } var iniFile = new INIFile(); iniFile._lines = lines; return iniFile; }
public static bool Write(string path, INIFile iniFile) { StreamWriter stream = null; try { stream = new StreamWriter(File.OpenWrite(path)); foreach (var line in iniFile._lines) { stream.WriteLine(line.ToString()); } } catch { return false; } finally { if (stream != null) stream.Close(); } return true; }