IniFile.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Masuit.Tools.Reflection;
  8. namespace Masuit.Tools.Files;
  9. /// <summary>
  10. /// INI文件操作辅助类
  11. /// </summary>
  12. public class IniFile
  13. {
  14. private readonly Dictionary<string, IniSection> _sections;
  15. private readonly StringComparer _stringComparer;
  16. private readonly BoolOptions _boolOptions;
  17. private readonly string _path;
  18. /// <summary>
  19. /// 全局配置节
  20. /// </summary>
  21. public const string DefaultSectionName = "General";
  22. /// <summary>
  23. ///
  24. /// </summary>
  25. /// <param name="path">文件路径</param>
  26. /// <param name="comparer"></param>
  27. /// <param name="boolOptions"></param>
  28. public IniFile(string path, StringComparer comparer = null, BoolOptions boolOptions = null)
  29. {
  30. _path = path;
  31. _stringComparer = comparer ?? StringComparer.CurrentCultureIgnoreCase;
  32. _sections = new Dictionary<string, IniSection>(_stringComparer ?? StringComparer.CurrentCultureIgnoreCase);
  33. _boolOptions = boolOptions ?? new BoolOptions();
  34. if (File.Exists(path))
  35. {
  36. using StreamReader reader = new(path, Encoding.UTF8);
  37. IniSection section = null;
  38. while (reader.ReadLine() is { } line)
  39. {
  40. ParseLine(line, ref section);
  41. }
  42. }
  43. }
  44. /// <summary>
  45. /// 重新加载文件
  46. /// </summary>
  47. public void Reload()
  48. {
  49. _sections.Clear();
  50. using StreamReader reader = new(_path, Encoding.UTF8);
  51. IniSection section = null;
  52. while (reader.ReadLine() is { } line)
  53. {
  54. ParseLine(line, ref section);
  55. }
  56. }
  57. private void ParseLine(string line, ref IniSection section)
  58. {
  59. if (string.IsNullOrWhiteSpace(line))
  60. {
  61. return;
  62. }
  63. line = line.Trim();
  64. if (line[0] == ';')
  65. {
  66. return;
  67. }
  68. if (line[0] == '[')
  69. {
  70. var name = line.Trim('[', ']').Trim();
  71. if (name.Length > 0 && !_sections.TryGetValue(name, out section))
  72. {
  73. section = new IniSection(name, _stringComparer);
  74. _sections.Add(section.Name, section);
  75. }
  76. }
  77. else
  78. {
  79. string name, value;
  80. var strs = line.Split(';')[0].Split('=');
  81. if (strs.Length == 1)
  82. {
  83. name = line.Trim();
  84. value = string.Empty;
  85. }
  86. else
  87. {
  88. name = strs[0].Trim();
  89. value = strs.Skip(1).Join("=").Trim();
  90. }
  91. if (name.Length <= 0)
  92. {
  93. return;
  94. }
  95. if (section == null)
  96. {
  97. section = new IniSection(DefaultSectionName, _stringComparer);
  98. _sections.Add(section.Name, section);
  99. }
  100. if (section.TryGetValue(name, out var item))
  101. {
  102. item.Value = value;
  103. }
  104. else
  105. {
  106. item = new IniItem
  107. {
  108. Name = name,
  109. Value = value
  110. };
  111. section.Add(name, item);
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// 保存配置文件
  117. /// </summary>
  118. public void Save()
  119. {
  120. using StreamWriter writer = new(_path, false, Encoding.UTF8);
  121. bool firstLine = true;
  122. foreach (var section in _sections.Values.Where(section => section.Count > 0))
  123. {
  124. if (firstLine)
  125. {
  126. firstLine = false;
  127. }
  128. else
  129. {
  130. writer.WriteLine();
  131. }
  132. writer.WriteLine($"[{section.Name}]");
  133. foreach (var setting in section.Values)
  134. {
  135. writer.WriteLine(setting.ToString());
  136. }
  137. }
  138. }
  139. /// <summary>
  140. /// 异步保存配置文件
  141. /// </summary>
  142. /// <returns></returns>
  143. public async Task SaveAsync()
  144. {
  145. using StreamWriter writer = new(_path, false, Encoding.UTF8);
  146. var firstLine = true;
  147. foreach (var section in _sections.Values.Where(section => section.Count > 0))
  148. {
  149. if (firstLine)
  150. {
  151. firstLine = false;
  152. }
  153. else
  154. {
  155. await writer.WriteLineAsync();
  156. }
  157. await writer.WriteLineAsync($"[{section.Name}]");
  158. foreach (var setting in section.Values)
  159. {
  160. await writer.WriteLineAsync(setting.ToString());
  161. }
  162. }
  163. }
  164. #region Read values
  165. /// <summary>
  166. /// 获取指定节的指定键的值
  167. /// </summary>
  168. /// <param name="section">节</param>
  169. /// <param name="key">键</param>
  170. /// <param name="defaultValue">获取不到时的默认值</param>
  171. /// <returns></returns>
  172. /// <exception cref="ArgumentNullException"></exception>
  173. public string GetValue(string section, string key, string defaultValue = null)
  174. {
  175. if (section == null)
  176. {
  177. throw new ArgumentNullException(nameof(section));
  178. }
  179. if (key == null)
  180. {
  181. throw new ArgumentNullException(nameof(key));
  182. }
  183. if (!_sections.TryGetValue(section, out var iniSection))
  184. {
  185. return defaultValue;
  186. }
  187. if (iniSection.TryGetValue(key, out var iniSetting))
  188. {
  189. return iniSetting.Value;
  190. }
  191. return defaultValue;
  192. }
  193. /// <summary>
  194. /// 获取指定节的指定键的值
  195. /// </summary>
  196. /// <typeparam name="T"></typeparam>
  197. /// <param name="section">配置节</param>
  198. /// <param name="key">键</param>
  199. /// <param name="defaultValue">获取不到时的默认值</param>
  200. /// <returns></returns>
  201. public T GetValue<T>(string section, string key, T defaultValue = default) where T : IConvertible
  202. {
  203. return GetValue(section, key).TryConvertTo(defaultValue);
  204. }
  205. /// <summary>
  206. /// 所有的配置节
  207. /// </summary>
  208. /// <returns></returns>
  209. public List<IniSection> GetSections() => _sections.Values.ToList();
  210. /// <summary>
  211. /// 获取指定节的所有键值对
  212. /// </summary>
  213. /// <param name="section">节</param>
  214. /// <returns></returns>
  215. /// <exception cref="ArgumentNullException"></exception>
  216. public Dictionary<string, string> GetSection(string section = DefaultSectionName)
  217. {
  218. if (section == null)
  219. {
  220. throw new ArgumentNullException(nameof(section));
  221. }
  222. var values = _sections.TryGetValue(section, out var iniSection) ? iniSection.Values : Enumerable.Empty<IniItem>();
  223. return values.ToDictionary(x => x.Name, x => x.Value);
  224. }
  225. /// <summary>
  226. /// 获取指定节的配置并绑定到指定类型
  227. /// </summary>
  228. /// <typeparam name="T"></typeparam>
  229. /// <param name="section">节</param>
  230. /// <returns></returns>
  231. public T GetSection<T>(string section = DefaultSectionName) where T : class, new()
  232. {
  233. var dic = GetSection(section);
  234. var obj = new T();
  235. var properties = typeof(T).GetProperties().ToDictionary(p => p.GetAttribute<IniPropertyAttribute>()?.Name ?? p.Name);
  236. foreach (var item in dic.Where(item => properties.ContainsKey(item.Key)))
  237. {
  238. properties[item.Key].SetValue(obj, item.Value.ConvertTo(properties[item.Key].PropertyType));
  239. }
  240. return obj;
  241. }
  242. #endregion Read values
  243. #region Write values
  244. /// <summary>
  245. /// 设置指定节的指定键的值
  246. /// </summary>
  247. /// <param name="section">节</param>
  248. /// <param name="key">键</param>
  249. /// <param name="value">值</param>
  250. /// <exception cref="ArgumentNullException"></exception>
  251. public void SetValue(string section, string key, string value)
  252. {
  253. if (section == null)
  254. {
  255. throw new ArgumentNullException(nameof(section));
  256. }
  257. if (key == null)
  258. {
  259. throw new ArgumentNullException(nameof(key));
  260. }
  261. if (!_sections.TryGetValue(section, out var sec))
  262. {
  263. sec = new IniSection(section, _stringComparer);
  264. _sections.Add(sec.Name, sec);
  265. }
  266. if (!sec.TryGetValue(key, out var item))
  267. {
  268. item = new IniItem
  269. {
  270. Name = key
  271. };
  272. sec.Add(key, item);
  273. }
  274. item.Value = value ?? string.Empty;
  275. }
  276. /// <summary>
  277. /// 设置指定节的指定键的值
  278. /// </summary>
  279. /// <param name="section">节</param>
  280. /// <param name="key">键</param>
  281. /// <param name="value">值</param>
  282. public void SetValue(string section, string key, bool value) => SetValue(section, key, _boolOptions.ToString(value));
  283. /// <summary>
  284. /// 设置指定节的指定键的值
  285. /// </summary>
  286. /// <param name="section">节</param>
  287. /// <param name="key">键</param>
  288. /// <param name="value">值</param>
  289. public void SetValue<T>(string section, string key, T value) where T : IConvertible => SetValue(section, key, value.ToString());
  290. /// <summary>
  291. /// 清空配置节
  292. /// </summary>
  293. /// <param name="section"></param>
  294. public void ClearSection(string section)
  295. {
  296. if (_sections.TryGetValue(section, out var sec))
  297. {
  298. sec.Clear();
  299. }
  300. }
  301. /// <summary>
  302. /// 清空所有配置节
  303. /// </summary>
  304. public void ClearAllSection()
  305. {
  306. _sections.Clear();
  307. }
  308. #endregion Write values
  309. }