public void SetValue(string section, string key, string value) { IniSection _inisection; if (this.o_Sections.TryGetValue(section, out _inisection)) { if (_inisection.IniKeyValues.TryGetValue(key, out var _inikey)) { _inikey.Value = value; } else { _inisection.IniKeyValues.TryAdd(key, new IniKeyValue(value)); } } else { _inisection = new IniSection(); this.o_Sections.TryAdd(section, _inisection); _inisection.IniKeyValues.TryAdd(key, new IniKeyValue(value)); } }
private void ReadIniFromTextStream(System.IO.TextReader Stream, IniReadErrorEventHandler IniReadErrorCallback) { StringBuilder lineBuffer = new StringBuilder(); int lineCount = 0; string pun = null, anothertmpStr; string[] splitBuffer = null; IniSection sectionBuffer = null; char[] buffer = new char[1]; char[] spli = new char[] { '=' }; DuplicatedKeyCollection dkc = new DuplicatedKeyCollection(); /*try * {//*/ int buffercount = Stream.ReadBlock(buffer, 0, 1); while (buffercount > 0) { switch (buffer[0]) { case ControlChars.Lf: lineCount++; pun = lineBuffer.ToString(); lineBuffer.Clear(); if (!string.IsNullOrWhiteSpace(pun)) { // move this line here because no need to check for every read char if (pun.StartsWith("[") && pun.EndsWith("]")) { anothertmpStr = pun.Substring(1, pun.Length - 2); sectionBuffer = new IniSection(false); if (!this.o_Sections.TryAdd(anothertmpStr, sectionBuffer)) { dkc.Add(anothertmpStr, lineCount, KeyType.Section); } } else if (pun.IndexOf(spli[0]) > -1) { if (sectionBuffer != null) { splitBuffer = pun.Split(spli, 2); bool isComment = false; if (splitBuffer[0].StartsWith(";")) { isComment = true; splitBuffer[0] = splitBuffer[0].Remove(0, 1); } if (!string.IsNullOrWhiteSpace(splitBuffer[0])) { anothertmpStr = splitBuffer[0].Trim(); if (splitBuffer.Length == 1) { if (!sectionBuffer.IniKeyValues.TryAdd(anothertmpStr, new IniKeyValue(string.Empty, isComment))) { dkc.Add(anothertmpStr, lineCount, KeyType.KeyValue); } } else { if (string.IsNullOrEmpty(splitBuffer[1])) { if (!sectionBuffer.IniKeyValues.TryAdd(anothertmpStr, new IniKeyValue(string.Empty, isComment))) { dkc.Add(anothertmpStr, lineCount, KeyType.KeyValue); } } else { if (string.IsNullOrWhiteSpace(splitBuffer[1])) { if (!sectionBuffer.IniKeyValues.TryAdd(anothertmpStr, new IniKeyValue(string.Empty, isComment))) { dkc.Add(anothertmpStr, lineCount, KeyType.KeyValue); } } else { if (!sectionBuffer.IniKeyValues.TryAdd(anothertmpStr, new IniKeyValue(this.UnescapeValue(splitBuffer[1].Trim()), isComment))) { dkc.Add(anothertmpStr, lineCount, KeyType.KeyValue); } } } } } } } else { dkc.Add(this.UnescapeValue(pun), lineCount, KeyType.Unknown); } } break; case ControlChars.Cr: break; case ControlChars.NullChar: break; default: lineBuffer.Append(buffer[0]); break; } buffercount = Stream.ReadBlock(buffer, 0, 1); } pun = lineBuffer.ToString(); if (!string.IsNullOrWhiteSpace(pun)) { //This will make sure last line without \n will not be discarded if (pun.StartsWith("[") && pun.EndsWith("]")) { anothertmpStr = pun.Substring(1, lineBuffer.Length - 2); sectionBuffer = new IniSection(false); if (!this.o_Sections.TryAdd(anothertmpStr, sectionBuffer)) { dkc.Add(anothertmpStr, lineCount, KeyType.Section); } lineBuffer.Clear(); } else if (pun.IndexOf(spli[0]) > -1) { splitBuffer = pun.Split(spli, 2); anothertmpStr = splitBuffer[0].Trim(); // make sure it split just one time if (!sectionBuffer.IniKeyValues.TryAdd(anothertmpStr, new IniKeyValue(this.UnescapeValue(splitBuffer[1].Trim())))) { dkc.Add(anothertmpStr, lineCount, KeyType.KeyValue); } lineBuffer.Clear(); } else { dkc.Add(this.UnescapeValue(pun), lineCount, KeyType.Unknown); } } if (dkc.Count > 0) { if (IniReadErrorCallback != null) { IniReadErrorCallback.Invoke(this, new IniReadErrorEventArgs(dkc)); } } /*} * catch (Exception ex) * { * if (IniReadErrorCallback != null) * { * if (dkc.Count > 0) * IniReadErrorCallback.Invoke(this, new IniReadErrorEventArgs(dkc, ex)); * else * IniReadErrorCallback.Invoke(this, new IniReadErrorEventArgs(ex)); * } * }//*/ spli = null; sectionBuffer = null; lineBuffer = null; splitBuffer = null; }