IniOperator.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Reflection;
  7. using IniParser;
  8. using IniParser.Model;
  9. using Utilities;
  10. namespace SAGA.DotNetUtils.Utilities
  11. {
  12. public class IniOperator
  13. {
  14. private IniOperator()
  15. {
  16. if (!File.Exists(IniPath))
  17. {
  18. FileInfo fileInfo = new FileInfo(IniPath);
  19. if (!fileInfo.Exists)
  20. {
  21. var parentDir = fileInfo.Directory;
  22. if (parentDir != null)
  23. {
  24. if (!parentDir.Exists)
  25. {
  26. parentDir.Create();
  27. }
  28. var fileStream = File.Create(IniPath);
  29. fileStream.Dispose();
  30. }
  31. }
  32. }
  33. }
  34. private static string IniPath { get; set; }
  35. private static string DefaultPath = Path.Combine(AppBaseInfo.AppTempFilePath, "Settings\\Settings.ini");
  36. public static IniOperator Instance(string path)
  37. {
  38. IniPath = path;
  39. return new IniOperator();
  40. }
  41. public static IniOperator Instance()
  42. {
  43. return Instance(DefaultPath);
  44. }
  45. public void SetSetting(string key, string value)
  46. {
  47. SetData("Settings", key, value);
  48. }
  49. public string GetSetting(string key)
  50. {
  51. return GetData("Settings", key);
  52. }
  53. public void SetData(string section, string key, string value)
  54. {
  55. var parser = new FileIniDataParser();
  56. IniData data = parser.ReadFile(IniPath);
  57. if (string.IsNullOrEmpty(section) || string.IsNullOrEmpty(key)) return;
  58. data[section][key] = value + "";
  59. parser.WriteFile(IniPath, data);
  60. }
  61. /// <summary>
  62. /// 获取该部分下的所有Value
  63. /// </summary>
  64. /// <param name="section"></param>
  65. /// <returns></returns>
  66. public Dictionary<string, string> GetData(string section)
  67. {
  68. Dictionary<string, string> dic = new Dictionary<string, string>();
  69. var parser = new FileIniDataParser();
  70. IniData data = parser.ReadFile(IniPath);
  71. if (string.IsNullOrEmpty(section)) return dic;
  72. if (!data.Sections.ContainsSection(section)) return dic;
  73. data[section].ToList().ForEach(t => dic.Add(t.KeyName, t.Value));
  74. return dic;
  75. }
  76. public string GetData(string section, string key)
  77. {
  78. var parser = new FileIniDataParser();
  79. IniData data = parser.ReadFile(IniPath);
  80. if (string.IsNullOrEmpty(section) || string.IsNullOrEmpty(key)) return "";
  81. if (!data.Sections.ContainsSection(section) || !data[section].ContainsKey(key)) return "";
  82. return data[section][key];
  83. }
  84. public void SaveData<T>(T t)
  85. {
  86. var parser = new FileIniDataParser();
  87. IniData data = parser.ReadFile(IniPath);
  88. Type type = t.GetType();
  89. var className = type.Name;
  90. System.Reflection.PropertyInfo[] propertyInfos = type.GetProperties();
  91. foreach (var propertyInfo in propertyInfos)
  92. {
  93. var name = propertyInfo.Name;
  94. var value = propertyInfo.GetValue(t);
  95. //if (propertyInfo.PropertyType == typeof(List<string>))
  96. if (value is List<string> strList)
  97. {
  98. data[className][name] = string.Join(",", strList);
  99. }
  100. else
  101. {
  102. data[className][name] = value + "";
  103. }
  104. }
  105. parser.WriteFile(IniPath, data);
  106. }
  107. public T GetData<T>() where T : class, new()
  108. {
  109. var parser = new FileIniDataParser();
  110. IniData data = parser.ReadFile(IniPath);
  111. Type type = typeof(T);
  112. Assembly asm = type.Assembly;
  113. var obj = asm.CreateInstance(type.FullName);
  114. var className = type.Name;
  115. PropertyInfo[] propertyInfos = type.GetProperties();
  116. foreach (var propertyInfo in propertyInfos)
  117. {
  118. var name = propertyInfo.Name;
  119. object value = null;
  120. if (propertyInfo.PropertyType == typeof(List<string>))
  121. {
  122. List<string> d = new List<string>();
  123. string v = data[className][name];
  124. if (!string.IsNullOrEmpty(v))
  125. {
  126. d.AddRange(v.Split(','));
  127. value = d;
  128. }
  129. }
  130. else
  131. {
  132. value = data[className][name];
  133. }
  134. propertyInfo.SetValue(obj, value);
  135. }
  136. return obj as T;
  137. }
  138. }
  139. }