DirectoryHelper.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Vinno.IUS.Common.Log;
  6. namespace Vinno.FIS.Sonopost.Helpers
  7. {
  8. internal class DirectoryHelper
  9. {
  10. /// <summary>
  11. /// Move directory between different path
  12. /// </summary>
  13. /// <param name="sourcePath">The source path</param>
  14. /// <param name="destPath">The destination path</param>
  15. /// <returns></returns>
  16. public static bool MoveDirectory(string sourcePath, string destPath)
  17. {
  18. try
  19. {
  20. var succes = CopyDirectory(sourcePath, destPath);
  21. if (succes)
  22. {
  23. DeleteDirectory(sourcePath);
  24. }
  25. else
  26. {
  27. return false;
  28. }
  29. }
  30. catch (Exception e)
  31. {
  32. Logger.WriteLineError($"Move directory {sourcePath} to {destPath} error:{e}");
  33. return false;
  34. }
  35. return true;
  36. }
  37. /// <summary>
  38. /// Move directory between different path
  39. /// </summary>
  40. /// <param name="sourcePath">The source path</param>
  41. /// <param name="destPath">The destination path</param>
  42. /// <returns></returns>
  43. public static bool MoveDirectoryContent(string sourcePath, string destPath)
  44. {
  45. try
  46. {
  47. var succes = CopyDirectoryContent(sourcePath, destPath);
  48. if (succes)
  49. {
  50. DeleteDirectory(sourcePath);
  51. }
  52. else
  53. {
  54. return false;
  55. }
  56. }
  57. catch (Exception e)
  58. {
  59. Logger.WriteLineError($"Move directory {sourcePath} to {destPath} error:{e}");
  60. return false;
  61. }
  62. return true;
  63. }
  64. /// <summary>
  65. /// Copy directory and files
  66. /// </summary>
  67. /// <param name="sourcePath">The source path</param>
  68. /// <param name="destPath">The destination path</param>
  69. public static bool CopyDirectory(string sourcePath, string destPath)
  70. {
  71. try
  72. {
  73. DirectoryInfo sourceInfo = new DirectoryInfo(sourcePath);
  74. var destFolder = Path.Combine(destPath, sourceInfo.Name);
  75. CreateDirectory(destFolder);
  76. foreach (FileInfo file in sourceInfo.GetFiles())
  77. {
  78. file.CopyTo(Path.Combine(destFolder, file.Name), true);
  79. }
  80. foreach (DirectoryInfo dirinfo in sourceInfo.GetDirectories())
  81. {
  82. CopyDirectory(dirinfo.FullName, destFolder);
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. Logger.WriteLineError($"Copy directory {sourcePath} to {destPath} error:{ex}");
  88. return false;
  89. }
  90. return true;
  91. }
  92. /// <summary>
  93. /// Copy directory content to another directory.
  94. /// </summary>
  95. /// <param name="sourcePath">The source path.</param>
  96. /// <param name="destPath">The destination path.</param>
  97. /// <returns></returns>
  98. public static bool CopyDirectoryContent(string sourcePath, string destPath)
  99. {
  100. try
  101. {
  102. if (Directory.Exists(sourcePath))
  103. {
  104. CreateDirectory(destPath);
  105. var sourceInfo = new DirectoryInfo(sourcePath);
  106. foreach (FileInfo file in sourceInfo.GetFiles())
  107. {
  108. file.CopyTo(Path.Combine(destPath, file.Name), true);
  109. }
  110. foreach (DirectoryInfo dirinfo in sourceInfo.GetDirectories())
  111. {
  112. var destFolder = Path.Combine(destPath, dirinfo.Name);
  113. CopyDirectoryContent(dirinfo.FullName, destFolder);
  114. }
  115. }
  116. }
  117. catch (Exception ex)
  118. {
  119. Logger.WriteLineError($"Copy directory {sourcePath} content to {destPath} error:{ex}");
  120. return false;
  121. }
  122. return true;
  123. }
  124. /// <summary>
  125. /// Copy directory and specific files
  126. /// </summary>
  127. /// <param name="sourcePath">The source path</param>
  128. /// <param name="destPath">The dest path</param>
  129. /// <param name="extensions">The file extensions</param>
  130. public static bool CopyDirectoryContent(string sourcePath, string destPath, IEnumerable<string> fileExtensions)
  131. {
  132. try
  133. {
  134. if (Directory.Exists(sourcePath))
  135. {
  136. CreateDirectory(destPath);
  137. var sourceInfo = new DirectoryInfo(sourcePath);
  138. foreach (FileInfo file in sourceInfo.GetFiles().Where(f => fileExtensions.Contains(f.Extension)))
  139. {
  140. file.CopyTo(Path.Combine(destPath, file.Name), true);
  141. }
  142. foreach (DirectoryInfo dirinfo in sourceInfo.GetDirectories())
  143. {
  144. var destFolder = Path.Combine(destPath, dirinfo.Name);
  145. CopyDirectoryContent(dirinfo.FullName, destFolder, fileExtensions);
  146. }
  147. }
  148. }
  149. catch (Exception ex)
  150. {
  151. Logger.WriteLineError($"Copy directory specific files from {sourcePath} to {destPath} error:{ex}");
  152. return false;
  153. }
  154. return true;
  155. }
  156. public static void CreateDirectory(string directory)
  157. {
  158. if (!Directory.Exists(directory))
  159. {
  160. Directory.CreateDirectory(directory);
  161. }
  162. }
  163. /// <summary>
  164. /// Delete directory
  165. /// </summary>
  166. /// <param name="directory"></param>
  167. public static bool DeleteDirectory(string directory)
  168. {
  169. try
  170. {
  171. if (!Directory.Exists(directory))
  172. {
  173. return true;
  174. }
  175. var files = Directory.GetFiles(directory);
  176. var directories = Directory.GetDirectories(directory);
  177. foreach (var file in files)
  178. {
  179. File.SetAttributes(file, FileAttributes.Normal);
  180. File.Delete(file);
  181. }
  182. foreach (var dir in directories)
  183. {
  184. DeleteDirectory(dir);
  185. }
  186. Directory.Delete(directory, false);
  187. }
  188. catch (Exception ex)
  189. {
  190. Logger.WriteLineError($"Delete directory {directory} error: {ex}");
  191. return false;
  192. }
  193. return true;
  194. }
  195. }
  196. }