FileHelper.cs 5.2 KB

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