using System; using System.Collections.Generic; using System.Linq; using System.IO; namespace Chop { public class FileReader { private readonly string _filePath; private readonly Dictionary _values; public FileReader(string FilePath) { _filePath = FilePath; _values = new Dictionary(); string[] configContent = File.ReadAllLines(_filePath); foreach (string line in configContent) { string[] split = line.Split('\''); if (split.Length > 1 && !split[0].Contains('#')) { string key = split[0].Substring(0, split[0].Length - 1).Replace("\\\'", "\'"); string content = split[1].Replace("\\\'", "\'"); _values.Add(key, content); } } } public string ReadValue(string key) { string[] content = File.ReadAllLines(_filePath); return (from line in content where line.Contains('=') where line.Split('=')[0] == key select line.Split('=')[1]).FirstOrDefault(); } public void EditValue(string key, string value) { var content = File.ReadAllLines(_filePath); for(int i = 0; i < content.Length; i++) { if (content[i].Contains('=')) { string[] s = content[i].Split('='); if (s[0] == key) s[1] = value; content[i] = s[0] + '=' + s[1]; } } File.WriteAllLines(_filePath, content); } public void AddValue(string key, string value) { File.AppendAllText(_filePath, Environment.NewLine + key + "=" + value); } public string GetString(string key) { if (_values.ContainsKey(key)) return _values[key]; return null; } public int GetInt(string key) { return int.Parse(GetString(key)); } public bool GetBoolean(string key) { return GetInt(key) == 1; } } }