using System; using System.Collections.Generic; using System.IO; namespace CMLeonOS.Utils { public class IniReader { private Dictionary> data = new Dictionary>(); public IniReader(string filePath) { if (File.Exists(filePath)) { string[] lines = File.ReadAllLines(filePath); string currentSection = ""; foreach (string line in lines) { string trimmedLine = line.Trim(); if (trimmedLine.StartsWith("[") && trimmedLine.EndsWith("]")) { currentSection = trimmedLine.Substring(1, trimmedLine.Length - 2); if (!data.ContainsKey(currentSection)) { data[currentSection] = new Dictionary(); } } else if (trimmedLine.Contains("=") && !string.IsNullOrWhiteSpace(currentSection)) { string[] parts = trimmedLine.Split(new[] { '=' }, 2); if (parts.Length == 2) { string key = parts[0].Trim(); string value = parts[1].Trim(); data[currentSection][key] = value; } } } } } public string GetValue(string section, string key, string defaultValue = "") { if (data.ContainsKey(section) && data[section].ContainsKey(key)) { return data[section][key]; } return defaultValue; } public int GetInt(string section, string key, int defaultValue = 0) { string value = GetValue(section, key, defaultValue.ToString()); if (int.TryParse(value, out int result)) { return result; } return defaultValue; } public bool GetBool(string section, string key, bool defaultValue = false) { string value = GetValue(section, key, defaultValue.ToString()).ToLower(); return value == "true" || value == "1" || value == "yes"; } public List GetSections() { return new List(data.Keys); } public List GetKeys(string section) { if (data.ContainsKey(section)) { return new List(data[section].Keys); } return new List(); } public bool TryReadBool(string key, out bool value, string section = "") { string sectionToUse = string.IsNullOrWhiteSpace(section) ? "General" : section; string stringValue = GetValue(sectionToUse, key, "false"); value = stringValue.ToLower() == "true" || stringValue == "1" || stringValue == "yes"; return true; } public bool TryReadInt(string key, out int value, string section = "") { string sectionToUse = string.IsNullOrWhiteSpace(section) ? "General" : section; string stringValue = GetValue(sectionToUse, key, "0"); return int.TryParse(stringValue, out value); } public bool TryReadFloat(string key, out float value, string section = "") { string sectionToUse = string.IsNullOrWhiteSpace(section) ? "General" : section; string stringValue = GetValue(sectionToUse, key, "0"); return float.TryParse(stringValue, out value); } } }