FileExtensions.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System.IO;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace ReleaseHelper.Utils
  5. {
  6. public static class FileExtensions
  7. {
  8. /// <summary>
  9. /// 生成文件的md5
  10. /// </summary>
  11. /// <param name="filename"></param>
  12. /// <returns></returns>
  13. private static string FilenameMutexKey(string filename)
  14. {
  15. return Functions.GenMd5(filename);
  16. }
  17. /// <summary>
  18. /// 读取所有行
  19. /// </summary>
  20. /// <param name="filename"></param>
  21. /// <param name="encoding"></param>
  22. /// <returns></returns>
  23. public static string[] ReadAllLines(string filename, Encoding encoding = null)
  24. {
  25. encoding = encoding ?? Encoding.UTF8;
  26. string key = FilenameMutexKey(filename);
  27. string[] txt = { };
  28. void act()
  29. {
  30. if (File.Exists(filename))
  31. {
  32. txt = File.ReadAllLines(filename, encoding);
  33. }
  34. }
  35. MutexExtensions.Exec(key, act);
  36. return txt;
  37. }
  38. /// <summary>
  39. /// 读取所有文本
  40. /// </summary>
  41. /// <param name="filename"></param>
  42. /// <param name="encoding"></param>
  43. /// <returns></returns>
  44. public static string ReadAllText(string filename, Encoding encoding = null)
  45. {
  46. encoding = encoding ?? Encoding.UTF8;
  47. string key = FilenameMutexKey(filename);
  48. string txt = string.Empty;
  49. void act()
  50. {
  51. if (File.Exists(filename))
  52. {
  53. txt = File.ReadAllText(filename, encoding);
  54. }
  55. }
  56. MutexExtensions.Exec(key, act);
  57. return txt;
  58. }
  59. /// <summary>
  60. /// 写入所有文本
  61. /// </summary>
  62. /// <param name="filename"></param>
  63. /// <param name="txt"></param>
  64. /// <param name="encoding"></param>
  65. public static void WriteAllText(string filename, string txt, Encoding encoding = null)
  66. {
  67. encoding = encoding ?? Encoding.UTF8;
  68. string key = FilenameMutexKey(filename);
  69. CheckDirectory(Path.GetDirectoryName(filename), true);
  70. void act()
  71. {
  72. File.WriteAllText(filename, txt, encoding);
  73. }
  74. MutexExtensions.Exec(key, act);
  75. }
  76. /// <summary>
  77. /// 写入二进制值
  78. /// </summary>
  79. /// <param name="filename"></param>
  80. /// <param name="bytes"></param>
  81. public static void WriteBytes(string filename, byte[] bytes)
  82. {
  83. if (bytes != null)
  84. {
  85. string key = FilenameMutexKey(filename);
  86. CheckDirectory(Path.GetDirectoryName(filename), true);
  87. void act()
  88. {
  89. File.WriteAllBytes(filename, bytes);
  90. }
  91. MutexExtensions.Exec(key, act);
  92. }
  93. }
  94. /// <summary>
  95. /// 读取所有二进制
  96. /// </summary>
  97. /// <param name="filename"></param>
  98. /// <returns></returns>
  99. public static byte[] ReadBytes(string filename)
  100. {
  101. string key = FilenameMutexKey(filename);
  102. byte[] txt = default;
  103. void act()
  104. {
  105. if (File.Exists(filename))
  106. {
  107. txt = File.ReadAllBytes(filename);
  108. }
  109. }
  110. MutexExtensions.Exec(key, act);
  111. return txt;
  112. }
  113. /// <summary>
  114. /// 写入所有行
  115. /// </summary>
  116. /// <param name="filename"></param>
  117. /// <param name="txt"></param>
  118. /// <param name="encoding"></param>
  119. public static void WriteAllLines(string filename, string[] txt, Encoding encoding = null)
  120. {
  121. encoding = encoding ?? Encoding.UTF8;
  122. string key = FilenameMutexKey(filename);
  123. CheckDirectory(Path.GetDirectoryName(filename), true);
  124. void act()
  125. {
  126. File.WriteAllLines(filename, txt, encoding);
  127. }
  128. MutexExtensions.Exec(key, act);
  129. }
  130. /// <summary>
  131. /// 添加所有文本
  132. /// </summary>
  133. /// <param name="filename"></param>
  134. /// <param name="txt"></param>
  135. /// <param name="encoding"></param>
  136. public static void AppendAllText(string filename, string txt, Encoding encoding = null)
  137. {
  138. encoding = encoding ?? Encoding.UTF8;
  139. string key = FilenameMutexKey(filename);
  140. CheckDirectory(Path.GetDirectoryName(filename), true);
  141. void act()
  142. {
  143. File.AppendAllText(filename, txt, encoding);
  144. }
  145. MutexExtensions.Exec(key, act);
  146. }
  147. /// <summary>
  148. /// 检查目录是否存在,如果不存在,将根据条件创建或不创建
  149. /// </summary>
  150. /// <param name="p"></param>
  151. /// <param name="create">如果目录不存在,是否创建</param>
  152. /// <returns></returns>
  153. public static bool CheckDirectory(string p, bool create)
  154. {
  155. bool res = false;
  156. if (!string.IsNullOrEmpty(p))
  157. {
  158. res = Directory.Exists(p);
  159. if (!res && create)
  160. {
  161. Directory.CreateDirectory(p);
  162. res = true;
  163. }
  164. }
  165. return res;
  166. }
  167. /// <summary>
  168. /// 在文件管理器中打开文件夹并选择文件
  169. /// </summary>
  170. /// <param name="filename"></param>
  171. public static bool OpenFileInExplore(string filename)
  172. {
  173. bool res = false;
  174. try
  175. {
  176. System.Diagnostics.Process.Start("Explorer.exe", $"/select, {filename}");
  177. res = true;
  178. }
  179. catch
  180. {
  181. }
  182. return res;
  183. }
  184. /// <summary>
  185. /// 获取制定路径文件的md5值
  186. /// </summary>
  187. /// <param name="filename"></param>
  188. /// <returns></returns>
  189. public static string FileMd5(string filename)
  190. {
  191. string res = "";
  192. try
  193. {
  194. using (FileStream file = new FileStream(filename, FileMode.Open))
  195. using (MD5 md5 = new MD5CryptoServiceProvider())
  196. {
  197. byte[] retVal = md5.ComputeHash(file);
  198. StringBuilder sb = new StringBuilder();
  199. for (int i = 0; i < retVal.Length; i++)
  200. {
  201. sb.Append(retVal[i].ToString("x2"));
  202. }
  203. res = sb.ToString();
  204. }
  205. }
  206. catch
  207. {
  208. }
  209. return res;
  210. }
  211. }
  212. }