using System.Text; public class Ini { public Dictionary> ini = new Dictionary>(StringComparer.InvariantCultureIgnoreCase); string file; /// /// Initialize an INI file /// Load it if it exists /// /// Full path where the INI file has to be read from or written to public Ini(string file) { this.file = file; if (!File.Exists(file)) return; Load(); } /// /// Load the INI file content /// public void Load() { var txt = File.ReadAllText(file); Dictionary currentSection = new Dictionary(StringComparer.InvariantCultureIgnoreCase); ini[""] = currentSection; foreach (var l in txt.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries) .Select((t, i) => new { idx = i, text = t.Trim() })) // .Where(t => !string.IsNullOrWhiteSpace(t) && !t.StartsWith(";"))) { var line = l.text; if (line.StartsWith(";") || string.IsNullOrWhiteSpace(line)) { currentSection.Add(";" + l.idx.ToString(), line); continue; } if (line.StartsWith("[") && line.EndsWith("]")) { currentSection = new Dictionary(StringComparer.InvariantCultureIgnoreCase); ini[line.Substring(1, line.Length - 2)] = currentSection; continue; } var idx = line.IndexOf("="); if (idx == -1) currentSection[line] = ""; else currentSection[line.Substring(0, idx)] = line.Substring(idx + 1); } } /// /// Get a parameter value at the root level /// /// parameter key /// public string GetValue(string key) { return GetValue(key, "", ""); } /// /// Get a parameter value in the section /// /// parameter key /// section /// public string GetValue(string key, string section) { return GetValue(key, section, ""); } /// /// Returns a parameter value in the section, with a default value if not found /// /// parameter key /// section /// default value /// public string GetValue(string key, string section, string @default) { if (!ini.ContainsKey(section)) return @default; if (!ini[section].ContainsKey(key)) return @default; return ini[section][key]; } /// /// Save the INI file /// public void Save() { var sb = new StringBuilder(); foreach (var section in ini) { if (section.Key != "") { sb.AppendFormat("[{0}]", section.Key); sb.AppendLine(); } foreach (var keyValue in section.Value) { if (keyValue.Key.StartsWith(";")) { sb.Append(keyValue.Value); sb.AppendLine(); } else { sb.AppendFormat("{0}={1}", keyValue.Key, keyValue.Value); sb.AppendLine(); } } if (!endWithCRLF(sb)) sb.AppendLine(); } File.WriteAllText(file, sb.ToString()); } bool endWithCRLF(StringBuilder sb) { if (sb.Length < 4) return sb[sb.Length - 2] == '\r' && sb[sb.Length - 1] == '\n'; else return sb[sb.Length - 4] == '\r' && sb[sb.Length - 3] == '\n' && sb[sb.Length - 2] == '\r' && sb[sb.Length - 1] == '\n'; } /// /// Write a parameter value at the root level /// /// parameter key /// parameter value public void WriteValue(string key, string value) { WriteValue(key, "", value); } /// /// Write a parameter value in a section /// /// parameter key /// section /// parameter value public void WriteValue(string key, string section, string value) { Dictionary currentSection; if (!ini.ContainsKey(section)) { currentSection = new Dictionary(); ini.Add(section, currentSection); } else currentSection = ini[section]; currentSection[key] = value; } /// /// Get all the keys names in a section /// /// section /// public string[] GetKeys(string section) { if (!ini.ContainsKey(section)) return new string[0]; return ini[section].Keys.ToArray(); } /// /// Get all the section names of the INI file /// /// public string[] GetSections() { return ini.Keys.Where(t => t != "").ToArray(); } }