2 using Microsoft.VisualBasic;
5 using System.Collections.Generic;
9 using System.Text.RegularExpressions;
16 private Hashtable m_sections;
17 private ArrayList a_sections;
21 m_sections =
new Hashtable(StringComparer.InvariantCultureIgnoreCase);
22 a_sections =
new ArrayList();
26 public void Load(
string sFileName,
bool bMerge =
false)
34 StreamReader oReader =
new StreamReader(sFileName);
35 Regex regexcomment =
new Regex(
"^([\\s]*#.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
38 Regex regexsection =
new Regex(
"^[\\s]*\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\][\\s]*$", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
39 Regex regexkey =
new Regex(
"^\\s*([^=\\s]*)[^=]*=(.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
40 while (!oReader.EndOfStream)
42 string line = oReader.ReadLine();
43 if (line !=
string.Empty)
46 if (regexcomment.Match(line).Success)
48 m = regexcomment.Match(line);
49 Trace.WriteLine(
string.Format(
"Skipping Comment: {0}", m.Groups[0].Value));
51 else if (regexsection.Match(line).Success)
53 m = regexsection.Match(line);
54 Trace.WriteLine(
string.Format(
"Adding section [{0}]", m.Groups[1].Value));
57 else if (regexkey.Match(line).Success && tempsection != null)
59 m = regexkey.Match(line);
60 Trace.WriteLine(
string.Format(
"Adding Key [{0}]=[{1}]", m.Groups[1].Value, m.Groups[2].Value));
61 tempsection.
AddKey(m.Groups[1].Value).
Value = m.Groups[2].Value;
63 else if (tempsection != null)
66 Trace.WriteLine(
string.Format(
"Adding Key [{0}]", line));
72 Trace.WriteLine(
string.Format(
"Skipping unknown type of data: {0}", line));
80 public void Save(
string sFileName)
82 StreamWriter oWriter =
new StreamWriter(sFileName,
false);
85 Trace.WriteLine(
string.Format(
"Writing Section: [{0}]", s.
Name));
86 oWriter.WriteLine(
string.Format(
"[{0}]", s.
Name));
89 if (k.Value !=
string.Empty)
91 Trace.WriteLine(
string.Format(
"Writing Key: {0}={1}", k.Name, k.Value));
92 oWriter.WriteLine(
string.Format(
"{0}={1}", k.Name, k.Value));
96 Trace.WriteLine(
string.Format(
"Writing Key: {0}", k.Name));
97 oWriter.WriteLine(
string.Format(
"{0}", k.Name));
107 get {
return m_sections.Values; }
113 get {
return a_sections; }
120 sSection = sSection.Trim();
122 if (m_sections.ContainsKey(sSection))
129 m_sections[sSection] = s;
130 a_sections.Add(sSection);
138 sSection = sSection.Trim();
149 m_sections.Remove(Section.
Name);
154 Trace.WriteLine(ex.Message);
164 return (m_sections.Count == 0);
170 sSection = sSection.Trim();
172 if (m_sections.ContainsKey(sSection))
195 public bool SetKeyValue(
string sSection,
string sKey,
string sValue)
218 bRval = s.
SetName(sNewSection);
224 public bool RenameKey(
string sSection,
string sKey,
string sNewKey)
256 private string m_sSection;
259 private Hashtable m_keys;
264 m_sSection = sSection;
265 m_keys =
new Hashtable(StringComparer.InvariantCultureIgnoreCase);
271 get {
return m_keys.Values; }
277 get {
return m_sSection; }
285 if (sKey.Length != 0)
287 if (m_keys.ContainsKey(sKey))
313 m_keys.Remove(Key.
Name);
318 Trace.WriteLine(ex.Message);
328 return (m_keys.Count == 0);
335 if (m_keys.ContainsKey(sKey))
337 return (
IniKey)m_keys[sKey];
346 sSection = sSection.Trim();
347 if (sSection.Length != 0)
351 if (!
object.ReferenceEquals(s,
this) && s != null)
358 m_pIniFile.m_sections.Remove(m_sSection);
360 m_pIniFile.m_sections[sSection] =
this;
362 m_sSection = sSection;
367 Trace.WriteLine(ex.Message);
383 private string m_sKey;
385 private string m_sValue;
399 get {
return m_sKey; }
405 get {
return m_sValue; }
406 set { m_sValue = value; }
425 if (sKey.Length != 0)
428 if (!
object.ReferenceEquals(k,
this) && k != null)
435 m_section.m_keys.Remove(m_sKey);
437 m_section.m_keys[sKey] =
this;
444 Trace.WriteLine(ex.Message);
IniKey AddKey(string sKey)
bool RemoveSection(IniSection Section)
System.Collections.ICollection Keys
string GetKeyValue(string sSection, string sKey)
bool SetName(string sKey)
IniSection GetSection(string sSection)
bool RemoveKey(string sSection, string sKey)
bool RemoveKey(IniKey Key)
bool SetKeyValue(string sSection, string sKey, string sValue)
bool RemoveKey(string sKey)
bool RenameKey(string sSection, string sKey, string sNewKey)
bool RenameSection(string sSection, string sNewSection)
IniKey GetKey(string sKey)
IniSection AddSection(string sSection)
bool SetName(string sSection)
void SetValue(string sValue)
bool RemoveSection(string sSection)
void Save(string sFileName)
System.Collections.ICollection Sections
void Load(string sFileName, bool bMerge=false)