DirectoryHelper.cs 7.2 KB

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