FileHelper.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using Vinno.IUS.Common.Log;
  7. namespace Vinno.FIS.Sonopost.Helpers
  8. {
  9. public class FileHelper
  10. {
  11. /// <summary>
  12. /// Save file
  13. /// </summary>
  14. public static void Save(string path, string content)
  15. {
  16. var data = new UTF8Encoding().GetBytes(content);
  17. using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
  18. {
  19. stream.Write(data, 0, data.Length);
  20. }
  21. }
  22. public static string GetText(string path)
  23. {
  24. using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))//Felix:若改为ReadWrite,SmartLogs会提示文件被占用。
  25. {
  26. byte[] buffer = new byte[stream.Length];
  27. stream.Read(buffer, 0, buffer.Length);
  28. string text = Encoding.UTF8.GetString(buffer);
  29. return text;
  30. }
  31. }
  32. public static void CopyStream(string path, Stream destStream)
  33. {
  34. using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
  35. {
  36. fs.CopyTo(destStream);
  37. }
  38. }
  39. public static bool Exist(string path)
  40. {
  41. return File.Exists(path);
  42. }
  43. /// <summary>
  44. /// Write content to file.
  45. /// </summary>
  46. /// <param name="path">The file path.</param>
  47. /// <param name="content">The content.</param>
  48. /// <param name="encoding">The encoding type.</param>
  49. /// <param name="mode">The file mode.</param>
  50. /// <param name="fileShare">The file share mode.</param>
  51. public static void WriteFile(string path, string content, Encoding encoding, FileMode mode = FileMode.Create, FileShare fileShare = FileShare.None)
  52. {
  53. using (FileStream fs = new FileStream(path, mode, FileAccess.Write, fileShare))
  54. {
  55. byte[] bytes = encoding.GetBytes(content);
  56. fs.Write(bytes, 0, bytes.Length);
  57. }
  58. }
  59. /// <summary>
  60. /// Write string content to file.
  61. /// </summary>
  62. /// <param name="filePath">The file path.</param>
  63. /// <param name="content">The string contnet.</param>
  64. public static void WriteFile(string filePath, string content)
  65. {
  66. using (var sw = new StreamWriter(filePath, false))
  67. {
  68. sw.Write(content);
  69. }
  70. }
  71. /// <summary>
  72. /// Write bytes to file.
  73. /// </summary>
  74. /// <param name="path">The file path.</param>
  75. /// <param name="content">The bytes content.</param>
  76. public static void WriteFile(string path, byte[] bytes)
  77. {
  78. using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.None))
  79. {
  80. fs.Write(bytes, 0, bytes.Length);
  81. }
  82. }
  83. /// <summary>
  84. /// Read file with string content.
  85. /// </summary>
  86. /// <param name="filePath">The file path.</param>
  87. /// <returns>The</returns>
  88. public static string ReadStringFromFile(string filePath)
  89. {
  90. return ReadStringFromFile(filePath, Encoding.Default);
  91. }
  92. /// <summary>
  93. /// Read file with string content.
  94. /// </summary>
  95. /// <param name="filePath">The file path.</param>
  96. /// <returns></returns>
  97. public static string ReadStringFromFile(string filePath, Encoding encoding)
  98. {
  99. return encoding.GetString(ReadBytesFromFile(filePath));
  100. }
  101. /// <summary>
  102. /// Read bytes from file.
  103. /// </summary>
  104. /// <param name="filePath">The file path</param>
  105. /// <returns></returns>
  106. public static byte[] ReadBytesFromFile(string filePath)
  107. {
  108. if (File.Exists(filePath))
  109. {
  110. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  111. {
  112. byte[] buffur = new byte[fs.Length];
  113. fs.Read(buffur, 0, (int)fs.Length);
  114. return buffur;
  115. }
  116. }
  117. else
  118. {
  119. return null;
  120. }
  121. }
  122. /// <summary>
  123. /// Gets File Size (B)
  124. /// </summary>
  125. /// <param name="filePath"></param>
  126. /// <returns></returns>
  127. public static long GetFileSizeB(string filePath)
  128. {
  129. FileInfo fileInfo = new FileInfo(filePath);
  130. return fileInfo.Length % 1024;
  131. }
  132. /// <summary>
  133. /// Gets File Size (KB)
  134. /// </summary>
  135. /// <param name="filePath"></param>
  136. /// <returns></returns>
  137. public static long GetFileSizeKB(string filePath)
  138. {
  139. FileInfo fileInfo = new FileInfo(filePath);
  140. return fileInfo.Length / (1024 * 1024);
  141. }
  142. /// <summary>
  143. /// Gets File Size (MB)
  144. /// </summary>
  145. /// <param name="filePath"></param>
  146. /// <returns></returns>
  147. public static long GetFileSizeMB(string filePath)
  148. {
  149. FileInfo fileInfo = new FileInfo(filePath);
  150. return fileInfo.Length / (1024 * 1024 * 1024);
  151. }
  152. public static string GetFileSizeString(double length)
  153. {
  154. string[] sizes = { "B", "KB", "MB", "GB" };
  155. int order = 0;
  156. while (length >= 1024 && (order + 1) < sizes.Length)
  157. {
  158. order++;
  159. length /= 1024;
  160. }
  161. string filesize = string.Format("{0:0.##} {1}", length, sizes[order]);
  162. return filesize;
  163. }
  164. /// <summary>
  165. /// Cope file.
  166. /// </summary>
  167. /// <param name="sourceFilePath">The source file.</param>
  168. /// <param name="destFilePath">The destination file.</param>
  169. public static void CopyFile(string sourceFilePath, string destFilePath, bool overwrite)
  170. {
  171. try
  172. {
  173. if (File.Exists(sourceFilePath))
  174. {
  175. var newDirectory = new FileInfo(destFilePath).DirectoryName;
  176. DirectoryHelper.CreateDirectory(newDirectory);
  177. File.Copy(sourceFilePath, destFilePath, overwrite);
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. Logger.WriteLineError($"Copy file {sourceFilePath} to {destFilePath} error,detail:{ex}");
  183. }
  184. }
  185. /// <summary>
  186. /// Cope file.
  187. /// </summary>
  188. /// <param name="sourceFilePath">The source file.</param>
  189. /// <param name="destFilePath">The destination file.</param>
  190. public static void MoveFile(string sourceFilePath, string destFilePath, bool overwrite)
  191. {
  192. try
  193. {
  194. if (File.Exists(sourceFilePath))
  195. {
  196. var newDirectory = new FileInfo(destFilePath).DirectoryName;
  197. DirectoryHelper.CreateDirectory(newDirectory);
  198. File.Copy(sourceFilePath, destFilePath, overwrite);
  199. DeleteFile(sourceFilePath);
  200. }
  201. }
  202. catch (Exception ex)
  203. {
  204. Logger.WriteLineError($"Copy file {sourceFilePath} to {destFilePath} error,detail:{ex}");
  205. }
  206. }
  207. public static IEnumerable<FileInfo> GetFilesByExtension(string folderPath, string extension)
  208. {
  209. if (Directory.Exists(folderPath))
  210. {
  211. var uploadReportDir = new DirectoryInfo(folderPath);
  212. return uploadReportDir.GetFiles().Where(f => f.Extension == extension);
  213. }
  214. return new FileInfo[0];
  215. }
  216. /// <summary>
  217. /// Delete file
  218. /// </summary>
  219. /// <param name="filePath">The file to be deleted.</param>
  220. public static void DeleteFile(string filePath)
  221. {
  222. if (string.IsNullOrEmpty(filePath))
  223. {
  224. return;
  225. }
  226. try
  227. {
  228. if (File.Exists(filePath))
  229. {
  230. File.SetAttributes(filePath, FileAttributes.Normal);
  231. File.Delete(filePath);
  232. }
  233. }
  234. catch (Exception ex)
  235. {
  236. Logger.WriteLineError($"Exception when delete file {filePath},detail:{ex}");
  237. }
  238. }
  239. }
  240. }