public bool TryRead(string section, string key, string defaultValue, out string value) { value = defaultValue; IniSectionNode iniSectionNode = null; if (!m_sectionDictionary.TryGetValue(section.ToLower(), out iniSectionNode)) { #if WRITE_DEFAULT Write(section, key, defaultValue); #endif return(false); } IniKeyNode iniKeyNode = null; if (!iniSectionNode.KeyDictionary.TryGetValue(key.ToLower(), out iniKeyNode)) { #if WRITE_DEFAULT Write(section, key, defaultValue); #endif return(false); } value = iniKeyNode.Value; return(true); }
private void ReadIni(string[] lineArray, Dictionary <string, IniSectionNode> sectionDictionary) { IniSectionNode sectionNode = null; char[] charArray = new char[] { '=' }; List <string> textList = new List <string>(); foreach (string line in lineArray) { string strLine = line.Trim(); if (String.IsNullOrEmpty(line) || strLine.StartsWith(";")) { textList.Add(line); continue; } if (strLine.StartsWith("[") && strLine.EndsWith("]")) { string strSection = strLine.Substring(1, strLine.Length - 2); if (!sectionDictionary.TryGetValue(strSection.ToLower(), out sectionNode)) { sectionNode = new IniSectionNode(textList.ToArray(), strSection); sectionDictionary.Add(strSection.ToLower(), sectionNode); textList.Clear(); } continue; } if (strLine.Contains("=")) { if (sectionNode != null) { string[] splitArray = strLine.Split(charArray, 2); if (splitArray.Length >= 2) { string key = splitArray[0].Trim(); string value = String.Join("=", splitArray, 1, splitArray.Length - 1); if (!sectionNode.KeyDictionary.ContainsKey(key.ToLower())) { IniKeyNode keyNode = new IniKeyNode(textList.ToArray(), key, value); sectionNode.KeyDictionary.Add(key.ToLower(), keyNode); textList.Clear(); } } } continue; } textList.Add(line); } }
public bool Write(string section, string key, string value) { if (key == null && value == null) { return(TryDeleteSection(section)); } if (value == null) { return(TryDeleteKey(section, key)); } IniSectionNode sectionNode = null; if (m_sectionDictionary.TryGetValue(section.ToLower(), out sectionNode)) { IniKeyNode valueNode = null; if (sectionNode.KeyDictionary.TryGetValue(key.ToLower(), out valueNode)) { valueNode.Value = value; } else { valueNode = new IniKeyNode(key, value); sectionNode.KeyDictionary.Add(key.ToLower(), valueNode); } } else { sectionNode = new IniSectionNode(section); sectionNode.KeyDictionary.Add(key.ToLower(), new IniKeyNode(key, value)); m_sectionDictionary.Add(section.ToLower(), sectionNode); } m_fileChanged = true; return(true); }