public static INIPart[] completeINI(string FileName) { List <INIPart> parts = new List <INIPart>(); if (File.Exists(FileName)) { INIPart p = new INIPart(); foreach (string Line in File.ReadAllLines(FileName)) { //Filter invalid Lines if (Line.Length > 0 && !Line.StartsWith(";")) { //New Section if (Line.StartsWith("[") && Line.EndsWith("]")) { if (p.Section != null) { parts.Add(p); p = new INIPart(); } p.Section = Line.Substring(1, Line.Length - 2); p.Settings = new NameValueCollection(); } else { //New Setting if (p.Section != null && Line.Contains("=")) { p.Settings.Add(Line.Split('=')[0].Trim(), Line.Split(new char[] { '=' }, 2)[1]); } } } } if (p.Section != null) { parts.Add(p); } return(parts.ToArray()); } return(null); }
public static INIPart[] completeINI(string FileName) { List<INIPart> parts = new List<INIPart>(); if (File.Exists(FileName)) { INIPart p = new INIPart(); foreach (string Line in File.ReadAllLines(FileName)) { //Filter invalid Lines if (Line.Length > 0 && !Line.StartsWith(";")) { //New Section if (Line.StartsWith("[") && Line.EndsWith("]")) { if (p.Section != null) { parts.Add(p); p = new INIPart(); } p.Section = Line.Substring(1, Line.Length - 2); p.Settings = new NameValueCollection(); } else { //New Setting if (p.Section != null && Line.Contains("=")) { p.Settings.Add(Line.Split('=')[0].Trim(), Line.Split(new char[] { '=' }, 2)[1]); } } } } if (p.Section != null) { parts.Add(p); } return parts.ToArray(); } return null; }
/// <summary> /// Rewrites a complete INI File. This erases Comments. /// </summary> /// <param name="FileName">File Name</param> /// <param name="Parts">INI Sections (aka. Parts)</param> public static void RewriteINI(string FileName, INIPart[] Parts) { if (Parts != null && Parts.Length>0) { var Content = string.Empty; foreach (INIPart p in Parts) { Content += string.Format("[{0}]\r\n",p.Section); foreach (var k in p.Settings.AllKeys) { Content += string.Format("{0}={1}\r\n", k, Program.toEmpty(p.Settings[k])); } Content += "\r\n"; } File.Delete(FileName); File.WriteAllText(FileName, Content.Trim()); } }