FileUtil.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Runtime.InteropServices;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. namespace Infrastructure
  9. {
  10. public class FileUtil
  11. {
  12. /// <summary>
  13. /// 按时间来创建文件夹
  14. /// </summary>
  15. /// <param name="path"></param>
  16. /// <returns>eg: /{yourPath}/2020/11/3/</returns>
  17. public static string GetdirPath(string path = "")
  18. {
  19. DateTime date = DateTime.Now;
  20. string timeDir = date.ToString("yyyyMMdd");// date.ToString("yyyyMM/dd/HH/");
  21. if (!string.IsNullOrEmpty(path))
  22. {
  23. timeDir = Path.Combine(path, timeDir);
  24. }
  25. return timeDir;
  26. }
  27. /// <summary>
  28. /// 取文件名的MD5值(16位)
  29. /// </summary>
  30. /// <param name="str">文件名,不包括扩展名</param>
  31. /// <returns></returns>
  32. public static string HashFileName(string str = null)
  33. {
  34. if (string.IsNullOrEmpty(str))
  35. {
  36. str = Guid.NewGuid().ToString();
  37. }
  38. MD5 md5 = MD5.Create();
  39. return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
  40. }
  41. /// <summary>
  42. /// 删除指定目录下的所有文件及文件夹(保留目录)
  43. /// </summary>
  44. /// <param name="file">文件目录</param>
  45. public static void DeleteDirectory(string file)
  46. {
  47. try
  48. {
  49. //判断文件夹是否还存在
  50. if (Directory.Exists(file))
  51. {
  52. DirectoryInfo fileInfo = new DirectoryInfo(file);
  53. //去除文件夹的只读属性
  54. fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
  55. foreach (string f in Directory.GetFileSystemEntries(file))
  56. {
  57. if (File.Exists(f))
  58. {
  59. //去除文件的只读属性
  60. File.SetAttributes(file, FileAttributes.Normal);
  61. //如果有子文件删除文件
  62. File.Delete(f);
  63. }
  64. else
  65. {
  66. //循环递归删除子文件夹
  67. DeleteDirectory(f);
  68. }
  69. }
  70. //删除空文件夹
  71. Directory.Delete(file);
  72. }
  73. }
  74. catch (Exception ex) // 异常处理
  75. {
  76. Console.WriteLine("代码生成异常" + ex.Message);
  77. }
  78. }
  79. /// <summary>
  80. /// 压缩代码
  81. /// </summary>
  82. /// <param name="zipPath"></param>
  83. /// <param name="genCodePath"></param>
  84. /// <param name="zipFileName">压缩后的文件名</param>
  85. /// <returns></returns>
  86. public static bool ZipGenCode(string zipPath, string genCodePath, string zipFileName)
  87. {
  88. if (string.IsNullOrEmpty(zipPath)) return false;
  89. try
  90. {
  91. CreateDirectory(genCodePath);
  92. string zipFileFullName = Path.Combine(zipPath, zipFileName);
  93. if (File.Exists(zipFileFullName))
  94. {
  95. File.Delete(zipFileFullName);
  96. }
  97. ZipFile.CreateFromDirectory(genCodePath, zipFileFullName);
  98. DeleteDirectory(genCodePath);
  99. return true;
  100. }
  101. catch (Exception ex)
  102. {
  103. Console.WriteLine("压缩文件出错。" + ex.Message);
  104. return false;
  105. }
  106. }
  107. /// <summary>
  108. /// 创建文件夹
  109. /// </summary>
  110. /// <param name="path"></param>
  111. /// <returns></returns>
  112. public static bool CreateDirectory(string path)
  113. {
  114. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  115. {
  116. path = path.Replace("\\", "/").Replace("//", "/");
  117. }
  118. try
  119. {
  120. if (!Directory.Exists(path))
  121. {
  122. DirectoryInfo info = Directory.CreateDirectory(path);
  123. Console.WriteLine("不存在创建文件夹" + info);
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. Console.WriteLine($"创建文件夹出错了,{ex.Message}");
  129. return false;
  130. }
  131. return true;
  132. }
  133. /// <summary>
  134. /// 写文件
  135. /// </summary>
  136. /// <param name="path">完整路径带扩展名的</param>
  137. /// <param name="content"></param>
  138. public static void WriteAndSave(string path, string content)
  139. {
  140. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  141. {
  142. path = path.Replace("\\", "/").Replace("//", "/");
  143. }
  144. if (!Directory.Exists(Path.GetDirectoryName(path)))
  145. {
  146. Directory.CreateDirectory(Path.GetDirectoryName(path));
  147. }
  148. Console.WriteLine("开始写入文件,Path=" + path);
  149. try
  150. {
  151. //实例化一个文件流--->与写入文件相关联
  152. using var fs = new FileStream(path, FileMode.Create, FileAccess.Write);
  153. //实例化一个StreamWriter-->与fs相关联
  154. using var sw = new StreamWriter(fs);
  155. //开始写入
  156. sw.Write(content);
  157. //清空缓冲区
  158. sw.Flush();
  159. //关闭流
  160. sw.Close();
  161. fs.Close();
  162. }
  163. catch (Exception ex)
  164. {
  165. Console.WriteLine("写入文件出错了:" + ex.Message);
  166. }
  167. }
  168. }
  169. }