IniFile.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections.Generic;
  2. using System.IO;
  3. namespace Wayne.Lib.IO
  4. {
  5. /// <summary>
  6. /// Class that manages an Ini-file.
  7. /// </summary>
  8. public class IniFile
  9. {
  10. private readonly IFileSupport fileSupport;
  11. /// <summary>
  12. /// Constructor.
  13. /// </summary>
  14. /// <param name="serviceLocator">The service locator.</param>
  15. public IniFile(IServiceLocator serviceLocator)
  16. {
  17. fileSupport = serviceLocator.GetService<IFileSupport>();
  18. Sections = new Dictionary<string, IniFileSection>();
  19. }
  20. /// <summary>
  21. /// The sections of the ini-file.
  22. /// </summary>
  23. public Dictionary<string, IniFileSection> Sections { get; private set; }
  24. /// <summary>
  25. /// Returns existing or creates a new section.
  26. /// </summary>
  27. /// <param name="sectionName">The name of the section.</param>
  28. /// <returns></returns>
  29. public IniFileSection GetSection(string sectionName)
  30. {
  31. IniFileSection section;
  32. if (!Sections.TryGetValue(sectionName, out section))
  33. {
  34. section = new IniFileSection();
  35. Sections.Add(sectionName, section);
  36. }
  37. return section;
  38. }
  39. /// <summary>
  40. /// Save ini file.
  41. /// </summary>
  42. /// <param name="fileName">The name of the file.</param>
  43. public void SaveToFile(string fileName)
  44. {
  45. using (Stream stream = fileSupport.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
  46. {
  47. using (StreamWriter streamWriter = new StreamWriter(stream))
  48. {
  49. foreach (KeyValuePair<string, IniFileSection> sectionValuePair in Sections)
  50. {
  51. streamWriter.WriteLine(string.Concat("[", sectionValuePair.Key, "]"));
  52. foreach (KeyValuePair<string, string> valuePair in sectionValuePair.Value.Values)
  53. {
  54. if (valuePair.Value != null)
  55. streamWriter.WriteLine(string.Concat(valuePair.Key.Trim(), "=", valuePair.Value.Trim()));
  56. else
  57. streamWriter.WriteLine(valuePair.Key.Trim());
  58. }
  59. streamWriter.WriteLine();
  60. }
  61. }
  62. }
  63. }
  64. /// <summary>
  65. /// Read an ini-file.
  66. /// </summary>
  67. /// <param name="fileName">The name of the file.</param>
  68. public void LoadFromFile(string fileName)
  69. {
  70. Sections.Clear();
  71. using (Stream stream = fileSupport.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  72. {
  73. using (StreamReader streamReader = new StreamReader(stream))
  74. {
  75. IniFileSection currentSection = null;
  76. while (!streamReader.EndOfStream)
  77. {
  78. string line = streamReader.ReadLine().Trim();
  79. if (!string.IsNullOrEmpty(line))
  80. {
  81. if (line.StartsWith("["))
  82. {
  83. if (line.EndsWith("]"))
  84. {
  85. string sectionName = line.Substring(1, line.Length - 2);
  86. currentSection = new IniFileSection();
  87. Sections[sectionName] = currentSection;
  88. }
  89. else
  90. {
  91. // Bad section name. Ignore this line.
  92. }
  93. }
  94. else if (currentSection != null)
  95. {
  96. int equalSignIndex = line.IndexOf('=');
  97. if (equalSignIndex > -1)
  98. {
  99. string key = line.Substring(0, equalSignIndex);
  100. string value = line.Substring(equalSignIndex + 1);
  101. currentSection.Values[key] = value;
  102. }
  103. else
  104. currentSection.Values[line] = null;
  105. }
  106. else
  107. {
  108. // No section has started. Ignore this line.
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116. /// <summary>
  117. /// A section of an Ini file.
  118. /// </summary>
  119. public class IniFileSection
  120. {
  121. /// <summary>
  122. /// Constructor.
  123. /// </summary>
  124. public IniFileSection()
  125. {
  126. Values = new Dictionary<string, string>();
  127. }
  128. /// <summary>
  129. /// The values of the section.
  130. /// </summary>
  131. public Dictionary<string, string> Values { get; private set; }
  132. }
  133. }