internal eFind Find(List<Section> mySections,string sSection, string Key, ref Section sect, ref Key xKey) { eFind eres = eFind.SECTION_NOT_FOUND; foreach (Section sec in mySections) { if (sec.Name.Equals(sSection)) { sect = sec; eres = eFind.KEY_NOT_FOUND; foreach (Key xkey in sec.Keys) { if (xkey.Name.Equals(Key)) { xKey = xkey; return eFind.OK; } } return eres; } } return eres; }
internal bool WritePrivateProfileString(string sSection, string Key, string sLiteralString, ref string Err) { Sections.Clear(); eReadSections eres = ReadSections(ref Sections, ref Err); switch (eres) { case eReadSections.OK: Key mykey = null; Section sect = null; eFind efres = Find(this.Sections,sSection,Key,ref sect, ref mykey); switch (efres) { case eFind.OK: mykey.sValue = sLiteralString; if (Write(ref Err)) { return true; } break; case eFind.SECTION_NOT_FOUND: sect = new Section(sSection); mykey = new Key(Key, sLiteralString); sect.Keys.Add(mykey); Sections.Add(sect); if (Write(ref Err)) { return true; } break; case eFind.KEY_NOT_FOUND: mykey = new Key(Key, sLiteralString); sect.Keys.Add(mykey); if (Write(ref Err)) { return true; } break; } return false; case eReadSections.FILE_NOT_EXISTS: sect = new Section(sSection); mykey = new Key(Key, sLiteralString); sect.Keys.Add(mykey); Sections.Add(sect); if (Write(ref Err)) { return true; } return false; case eReadSections.ERROR: return false; } return false; }
internal eReadSections ReadSections(ref List<Section> mySections, ref string Err) { Sections.Clear(); string mySectionName = null; Section CurrentSection = null; if (File.Exists(inifile)) { try { string[] sLines = File.ReadAllLines(inifile); foreach (string sline in sLines) { if (IsNewSection(ref mySectionName,sline)) { if (CurrentSection != null) { CurrentSection = new Section(mySectionName); mySections.Add(CurrentSection); } else { CurrentSection = new Section(mySectionName); mySections.Add(CurrentSection); } } else { string Key = null; string sValue = null; if (GetKeyAndValue(sline, ref Key, ref sValue)) { Key key = new Key(Key, sValue); CurrentSection.Keys.Add(key); } } } return eReadSections.OK; } catch (Exception ex) { Err = "ERROR:IniFile Exception = " + ex.Message; return eReadSections.ERROR; } } else { return eReadSections.FILE_NOT_EXISTS; } }