/IniUtility.cs

http://quick-fwknop-ssh-launcher.googlecode.com/ · C# · 185 lines · 117 code · 12 blank · 56 comment · 14 complexity · 0308fc2164d656002bafd94d519ee3fa MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.IO;
  7. namespace Quick_Knock
  8. {
  9. /// <summary>
  10. /// Read/Write .ini Files
  11. ///
  12. /// Version 1, 2009-08-15
  13. /// http://www.Stum.de
  14. /// </summary>
  15. /// <remarks>
  16. /// It supports the simple .INI Format:
  17. ///
  18. /// [SectionName]
  19. /// Key1=Value1
  20. /// Key2=Value2
  21. ///
  22. /// [Section2]
  23. /// Key3=Value3
  24. ///
  25. /// You can have empty lines (they are ignored), but comments are not supported
  26. /// Key4=Value4 ; This is supposed to be a comment, but will be part of Value4
  27. ///
  28. /// Whitespace is not trimmed from the beginning and end of either Key and Value
  29. ///
  30. /// Licensed under WTFPL
  31. /// http://sam.zoy.org/wtfpl/
  32. /// </remarks>
  33. public class IniFile
  34. {
  35. private Dictionary<string, Dictionary<string, string>> _iniFileContent;
  36. private readonly Regex _sectionRegex = new Regex(@"(?<=\[)(?<SectionName>[^\]]+)(?=\])");
  37. private readonly Regex _keyValueRegex = new Regex(@"(?<Key>[^=]+)=(?<Value>.+)");
  38. public IniFile() : this(null){}
  39. public IniFile(string filename)
  40. {
  41. _iniFileContent = new Dictionary<string, Dictionary<string, string>>();
  42. if (filename != null) Load(filename);
  43. }
  44. /// <summary>
  45. /// Get a specific value from the .ini file
  46. /// </summary>
  47. /// <param name="sectionName"></param>
  48. /// <param name="key"></param>
  49. /// <returns>The value of the given key in the given section, or NULL if not found</returns>
  50. public string GetValue(string sectionName, string key)
  51. {
  52. if (_iniFileContent.ContainsKey(sectionName) && _iniFileContent[sectionName].ContainsKey(key))
  53. return _iniFileContent[sectionName][key];
  54. else
  55. return null;
  56. }
  57. /// <summary>
  58. /// Set a specific value in a section
  59. /// </summary>
  60. /// <param name="sectionName"></param>
  61. /// <param name="key"></param>
  62. /// <param name="value"></param>
  63. public void SetValue(string sectionName, string key, string value)
  64. {
  65. if(!_iniFileContent.ContainsKey(sectionName)) _iniFileContent[sectionName] = new Dictionary<string, string>();
  66. _iniFileContent[sectionName][key] = value;
  67. }
  68. /// <summary>
  69. /// Get all the Values for a section
  70. /// </summary>
  71. /// <param name="sectionName"></param>
  72. /// <returns>A Dictionary with all the Key/Values for that section (maybe empty but never null)</returns>
  73. public Dictionary<string, string> GetSection(string sectionName)
  74. {
  75. if (_iniFileContent.ContainsKey(sectionName))
  76. return new Dictionary<string, string>(_iniFileContent[sectionName]);
  77. else
  78. return new Dictionary<string, string>();
  79. }
  80. /// <summary>
  81. /// Set an entire sections values
  82. /// </summary>
  83. /// <param name="sectionName"></param>
  84. /// <param name="sectionValues"></param>
  85. public void SetSection(string sectionName, IDictionary<string, string> sectionValues)
  86. {
  87. if (sectionValues == null) return;
  88. _iniFileContent[sectionName] = new Dictionary<string, string>(sectionValues);
  89. }
  90. /// <summary>
  91. /// Load an .INI File
  92. /// </summary>
  93. /// <param name="filename"></param>
  94. /// <returns></returns>
  95. public bool Load(string filename)
  96. {
  97. if (File.Exists(filename))
  98. {
  99. try
  100. {
  101. var content = File.ReadAllLines(filename);
  102. _iniFileContent = new Dictionary<string, Dictionary<string, string>>();
  103. string currentSectionName = string.Empty;
  104. foreach (var line in content)
  105. {
  106. Match m = _sectionRegex.Match(line);
  107. if (m.Success)
  108. {
  109. currentSectionName = m.Groups["SectionName"].Value;
  110. }
  111. else
  112. {
  113. m = _keyValueRegex.Match(line);
  114. if (m.Success)
  115. {
  116. string key = m.Groups["Key"].Value;
  117. string value = m.Groups["Value"].Value;
  118. Dictionary<string, string> kvpList;
  119. if (_iniFileContent.ContainsKey(currentSectionName))
  120. {
  121. kvpList = _iniFileContent[currentSectionName];
  122. }
  123. else
  124. {
  125. kvpList = new Dictionary<string, string>();
  126. }
  127. kvpList[key] = value;
  128. _iniFileContent[currentSectionName] = kvpList;
  129. }
  130. }
  131. }
  132. return true;
  133. }
  134. catch
  135. {
  136. return false;
  137. }
  138. }
  139. else
  140. {
  141. return false;
  142. }
  143. }
  144. /// <summary>
  145. /// Save the content of this class to an INI File
  146. /// </summary>
  147. /// <param name="filename"></param>
  148. /// <returns></returns>
  149. public bool Save(string filename)
  150. {
  151. var sb = new StringBuilder();
  152. if (_iniFileContent != null)
  153. {
  154. foreach (var sectionName in _iniFileContent)
  155. {
  156. sb.AppendFormat("[{0}]\r\n", sectionName.Key);
  157. foreach (var keyValue in sectionName.Value)
  158. {
  159. sb.AppendFormat("{0}={1}\r\n", keyValue.Key, keyValue.Value);
  160. }
  161. }
  162. }
  163. try
  164. {
  165. File.WriteAllText(filename, sb.ToString());
  166. return true;
  167. } catch
  168. {
  169. return false;
  170. }
  171. }
  172. }
  173. }