FileHelper.cs 5.1 KB

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