FileHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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;
  12. using TeamAAS_VP.Models;
  13. using TeamAAS_VP.Resources.Languages;
  14. using static Org.BouncyCastle.Math.EC.ECCurve;
  15. namespace TeamAAS_VP.Core
  16. {
  17. public static class FileHelper
  18. {
  19. /// <summary>
  20. /// 写入Json文件
  21. /// </summary>
  22. /// <param name="obj">对象</param>
  23. /// <param name="path">文件路径</param>
  24. public static void WriteJsonFile(object obj, string path)
  25. {
  26. if (!Directory.Exists(Path.GetDirectoryName(path)))
  27. {
  28. Directory.CreateDirectory(Path.GetDirectoryName(path));
  29. }
  30. using (StreamWriter sw = new StreamWriter(path))
  31. {
  32. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  33. sw.Write(JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented));
  34. }
  35. }
  36. /// <summary>
  37. /// 读取Json文件
  38. /// </summary>
  39. /// <typeparam name="T">对象</typeparam>
  40. /// <param name="path">文件路径</param>
  41. /// <returns></returns>
  42. /// <exception cref="Exception"></exception>
  43. public static T ReadJsonFile<T>(string path)
  44. {
  45. if (File.Exists(path))
  46. {
  47. string buffer;
  48. using (StreamReader sr = new StreamReader(path))
  49. {
  50. buffer = sr.ReadToEnd();
  51. }
  52. return JsonConvert.DeserializeObject<T>(buffer);
  53. }
  54. else
  55. {
  56. throw new Exception(string.Format(Lang.文件不存在0, path));
  57. }
  58. }
  59. /// <summary>
  60. /// 写入文件
  61. /// </summary>
  62. /// <param name="content">对象</param>
  63. /// <param name="path">文件路径</param>
  64. public static void WriteFile(string content, string path)
  65. {
  66. if (!Directory.Exists(Path.GetDirectoryName(path)))
  67. {
  68. Directory.CreateDirectory(Path.GetDirectoryName(path));
  69. }
  70. using (StreamWriter sw = new StreamWriter(path))
  71. {
  72. sw.Write(content);
  73. }
  74. }
  75. /// <summary>
  76. /// 读取文件
  77. /// </summary>
  78. /// <param name="path"></param>
  79. /// <returns></returns>
  80. /// <exception cref="Exception"></exception>
  81. public static string ReadFile(string path)
  82. {
  83. if (File.Exists(path))
  84. {
  85. string buffer;
  86. using (StreamReader sr = new StreamReader(path))
  87. {
  88. buffer = sr.ReadToEnd();
  89. }
  90. return buffer;
  91. }
  92. else
  93. {
  94. throw new Exception(string.Format(Lang.文件不存在0, path));
  95. }
  96. }
  97. /// <summary>
  98. /// 检测文件名是否合规
  99. /// </summary>
  100. /// <param name="filename"></param>
  101. /// <returns></returns>
  102. public static bool CheckFileName(string filename)
  103. {
  104. //
  105. // 定义文件名合法性的正则表达式
  106. string pattern = @"^[^-\\/:*?""<>|\x00-\x1F]*$"; // 匹配不包含非法字符的文件名
  107. // 使用正则表达式进行匹配
  108. if (Regex.IsMatch(filename, pattern))
  109. {
  110. //Console.WriteLine("输入的名称符合文件名命名规则,并且不包含非法字符。");
  111. return true;
  112. }
  113. else
  114. {
  115. //Console.WriteLine("输入的名称不符合文件名命名规则,或者包含非法字符。");
  116. return false;
  117. }
  118. }
  119. }
  120. }