FileHelper.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using DefaultEdit.Model;
  2. using EasyHttp.Infrastructure;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. namespace DefaultEdit.Core
  12. {
  13. public static class FileHelper
  14. {
  15. private static string ApplicationConfigurationPath="..//Config//ApplicationConfigurationPath.cfg";
  16. /// <summary>
  17. /// 写入Json文件
  18. /// </summary>
  19. /// <param name="obj">对象</param>
  20. /// <param name="path">文件路径</param>
  21. public static void WriteJsonFile(object obj, string path)
  22. {
  23. if ( !Directory.Exists(Path.GetDirectoryName(path)) )
  24. {
  25. Directory.CreateDirectory(Path.GetDirectoryName(path));
  26. }
  27. ////保存参数
  28. //JsonSerializer serializer = new JsonSerializer();
  29. //TextReader tr = new StringReader(JsonConvert.SerializeObject(obj));
  30. //JsonTextReader jtr = new JsonTextReader(tr);
  31. //object obj1 = serializer.Deserialize(jtr);
  32. //if (obj1 != null)
  33. //{
  34. // StringWriter textWriter = new StringWriter();
  35. // JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
  36. // {
  37. // Formatting = Formatting.Indented,
  38. // Indentation = 4,
  39. // IndentChar = ' '
  40. // };
  41. // serializer.Serialize(jsonWriter, obj1);
  42. // using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
  43. // {
  44. // using (StreamWriter sw = new StreamWriter(fs))
  45. // {
  46. // sw.Write(textWriter.ToString());
  47. // }
  48. // }
  49. //}
  50. //else
  51. //{
  52. // using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
  53. // {
  54. // using (StreamWriter sw = new StreamWriter(fs))
  55. // {
  56. // string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  57. // sw.Write(JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented));
  58. // }
  59. // }
  60. //}
  61. using ( StreamWriter sw = new StreamWriter(path) )
  62. {
  63. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  64. sw.Write(JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented));
  65. }
  66. }
  67. /// <summary>
  68. /// 读取Json文件
  69. /// </summary>
  70. /// <typeparam name="T">对象</typeparam>
  71. /// <param name="path">文件路径</param>
  72. /// <returns></returns>
  73. /// <exception cref="Exception"></exception>
  74. public static T ReadJsonFile<T>(string path)
  75. {
  76. if ( File.Exists(path) )
  77. {
  78. string buffer;
  79. using ( StreamReader sr = new StreamReader(path) )
  80. {
  81. buffer = sr.ReadToEnd();
  82. }
  83. return JsonConvert.DeserializeObject<T>(buffer);
  84. }
  85. else
  86. {
  87. throw new Exception($"文件不存在:[{path}]");
  88. }
  89. }
  90. /// <summary>
  91. /// 写入文件
  92. /// </summary>
  93. /// <param name="content">对象</param>
  94. /// <param name="path">文件路径</param>
  95. public static void WriteFile(string content, string path)
  96. {
  97. if ( !Directory.Exists(Path.GetDirectoryName(path)) )
  98. {
  99. Directory.CreateDirectory(Path.GetDirectoryName(path));
  100. }
  101. using ( StreamWriter sw = new StreamWriter(path) )
  102. {
  103. sw.Write(content);
  104. }
  105. }
  106. /// <summary>
  107. /// 读取文件
  108. /// </summary>
  109. /// <param name="path"></param>
  110. /// <returns></returns>
  111. /// <exception cref="Exception"></exception>
  112. public static string ReadFile(string path)
  113. {
  114. if ( File.Exists(path) )
  115. {
  116. string buffer;
  117. using ( StreamReader sr = new StreamReader(path) )
  118. {
  119. buffer = sr.ReadToEnd();
  120. }
  121. return buffer;
  122. }
  123. else
  124. {
  125. throw new Exception($"文件不存在:[{path}]");
  126. }
  127. }
  128. /// <summary>
  129. /// 读取软件设置参数
  130. /// </summary>
  131. /// <returns></returns>
  132. public static CurentApplicationSettings ReadApplicationConfiguration()
  133. {
  134. try
  135. {
  136. var config = FileHelper.ReadJsonFile<CurentApplicationSettings>(ApplicationConfigurationPath);
  137. return config;
  138. }
  139. catch ( Exception )
  140. {
  141. return new CurentApplicationSettings();
  142. }
  143. }
  144. /// <summary>
  145. /// 保存软件设置参数
  146. /// </summary>
  147. /// <param name="config"></param>
  148. public static void SaveApplicationConfiguration(CurentApplicationSettings config)
  149. {
  150. try
  151. {
  152. // config.DT = DateTime.Now;
  153. FileHelper.WriteJsonFile(config, ApplicationConfigurationPath);
  154. }
  155. catch ( Exception )
  156. {
  157. }
  158. }
  159. /// <summary>
  160. /// 检测文件名是否合规
  161. /// </summary>
  162. /// <param name="filename"></param>
  163. /// <returns></returns>
  164. public static bool CheckFileName(string filename)
  165. {
  166. //
  167. // 定义文件名合法性的正则表达式
  168. string pattern = @"^[^-\\/:*?""<>|\x00-\x1F]*$"; // 匹配不包含非法字符的文件名
  169. // 使用正则表达式进行匹配
  170. if ( Regex.IsMatch(filename, pattern) )
  171. {
  172. //Console.WriteLine("输入的名称符合文件名命名规则,并且不包含非法字符。");
  173. return true;
  174. }
  175. else
  176. {
  177. //Console.WriteLine("输入的名称不符合文件名命名规则,或者包含非法字符。");
  178. return false;
  179. }
  180. }
  181. }
  182. }