FileHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /// <param name="filename"></param>
  100. /// <returns></returns>
  101. public static bool CheckFileName(string filename)
  102. {
  103. //
  104. // 定义文件名合法性的正则表达式
  105. string pattern = @"^[^-\\/:*?""<>|\x00-\x1F]*$"; // 匹配不包含非法字符的文件名
  106. // 使用正则表达式进行匹配
  107. if (Regex.IsMatch(filename, pattern))
  108. {
  109. //Console.WriteLine("输入的名称符合文件名命名规则,并且不包含非法字符。");
  110. return true;
  111. }
  112. else
  113. {
  114. //Console.WriteLine("输入的名称不符合文件名命名规则,或者包含非法字符。");
  115. return false;
  116. }
  117. }
  118. }
  119. }