// Returns a IniKey object to the key by name, NULL if it was not found //public IniKey GetAddKey(string sKey) //{ // sKey = sKey.Trim(); // if (m_keys.ContainsKey(sKey)) // { // return (IniKey)m_keys[sKey]; // } // return AddKey(sKey); //} //public IniKey GetKey(string sKey) //{ // sKey = sKey.Trim(); // if (m_keys.ContainsKey(sKey)) // { // return (IniKey)m_keys[sKey]; // } // return null; //} // Sets the section name, returns true on success, fails if the section // name sSection already exists public bool SetName(string sSection) { sSection = sSection.Trim(); if (sSection.Length != 0) { // Get existing section if it even exists... IniSectionList s = m_pIniFile.GetSection(sSection); if (s != this && s != null) { return(false); } try { // Remove the current section m_pIniFile.m_sections.Remove(m_sSection); // Set the new section name to this object m_pIniFile.m_sections[sSection] = this; // Set the new section name m_sSection = sSection; return(true); } catch (Exception ex) { Trace.WriteLine(ex.Message); } } return(false); }
// Loads the Reads the data in the ini file into the IniFile object public void Load(string sFileName, bool bMerge) { if (sFileName == null) { return; } FilePath = sFileName; if (!bMerge) { RemoveAllSections(); } // Clear the object... IniSectionList tempsection = null; StreamReader oReader = new StreamReader(sFileName, Encoding.Default); Regex regexcomment = new Regex("^([\\s]*//.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); // ^[\\s]*\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\][\\s]*$ Regex regexsection = new Regex("^[\\s]*\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\][\\s]*$", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); //Regex regexsection = new Regex("\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\]", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); Regex regexkey = new Regex("^\\s*([^=\\s]*)[^=]*=(.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); while (!oReader.EndOfStream) { string line = oReader.ReadLine(); if (line != string.Empty) { Match m = null; if (regexcomment.Match(line).Success) { m = regexcomment.Match(line); Trace.WriteLine(string.Format("Skipping Comment: {0}", m.Groups[0].Value)); } else if (regexsection.Match(line).Success) { m = regexsection.Match(line); Trace.WriteLine(string.Format("Adding section [{0}]", m.Groups[1].Value)); tempsection = AddSection(m.Groups[1].Value); } else if (regexkey.Match(line).Success&& tempsection != null) { m = regexkey.Match(line); Trace.WriteLine(string.Format("Adding Key [{0}]=[{1}]", m.Groups[1].Value, m.Groups[2].Value)); tempsection.AddKey(m.Groups[1].Value, m.Groups[2].Value.Trim()); } else if (tempsection != null) { // Handle Key without value Trace.WriteLine(string.Format("Adding Key [{0}]", line)); tempsection.AddKey(line); } else { // This should not occur unless the tempsection is not created yet... Trace.WriteLine(string.Format("Skipping unknown type of data: {0}", line)); } } } oReader.Close(); }
// Returns a KeyValue in a certain section //public string GetKeyValue(string sSection, string sKey) //{ // IniSectionDuplicate s = GetSection(sSection); // if (s != null) // { // IniSectionDuplicate.IniKey k = s.GetAddKey(sKey); // if (k != null) // { // return k.Value; // } // } // return string.Empty; //} // Sets a KeyValuePair in a certain section //public bool SetKeyValue(string sSection, string sKey, string sValue) //{ // IniSectionDuplicate s = AddSection(sSection); // if (s != null) // { // IniSectionDuplicate.IniKey k = s.AddKey(sKey); // if (k != null) // { // k.Value = sValue; // return true; // } // } // return false; //} // Renames an existing section returns true on success, false if the section didn't exist or there was another section with the same sNewSection public bool RenameSection(string sSection, string sNewSection) { // Note string trims are done in lower calls. bool bRval = false; IniSectionList s = GetSection(sSection); if (s != null) { bRval = s.SetName(sNewSection); } return(bRval); }
// Removes section by object, returns trus on success public bool RemoveSection(IniSectionList Section) { if (Section != null) { try { m_sections.Remove(Section.Name); return(true); } catch (Exception ex) { Trace.WriteLine(ex.Message); } } return(false); }
// Adds a section to the IniFile object, returns a IniSection object to the new or existing object public IniSectionList AddSection(string sSection) { IniSectionList s = null; sSection = sSection.Trim(); // Trim spaces if (m_sections.ContainsKey(sSection)) { s = (IniSectionList)m_sections[sSection]; } else { s = new IniSectionList(this, sSection); m_sections[sSection] = s; } return(s); }
// Constuctor so objects are internally managed protected internal IniKey(IniSectionList parent, string sKey, string sValue) { m_section = parent; m_sKey = sKey; m_sValue = sValue; }