DirectoryEx.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.IO;
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup Utility
  7. * @{
  8. */
  9. /// <summary>
  10. /// Contains various methods that provide handling for directories not provided by System.Directory type.
  11. /// </summary>
  12. public static class DirectoryEx
  13. {
  14. /// <summary>
  15. /// Moves a directory from one path to another, while creating any parent directories if they don't already exist.
  16. /// </summary>
  17. /// <param name="source">Path to the directory to move.</param>
  18. /// <param name="destination">New location and/or name of the directory.</param>
  19. public static void Move(string source, string destination)
  20. {
  21. string destParent = PathEx.GetParent(destination);
  22. if (!string.IsNullOrEmpty(destParent))
  23. {
  24. if (!Directory.Exists(destParent))
  25. Directory.CreateDirectory(destParent);
  26. }
  27. Directory.Move(source, destination);
  28. }
  29. /// <summary>
  30. /// Recursively copies a directory from one path to another, while creating any parent directories if they don't
  31. /// already exist.
  32. /// </summary>
  33. /// <param name="source">Path to the directory to copy.</param>
  34. /// <param name="destination">Path determining where the directory copy will be placed.</param>
  35. public static void Copy(string source, string destination)
  36. {
  37. DirectoryInfo dir = new DirectoryInfo(source);
  38. DirectoryInfo[] dirs = dir.GetDirectories();
  39. if (!dir.Exists)
  40. {
  41. throw new DirectoryNotFoundException(
  42. "Source directory does not exist or could not be found: "
  43. + source);
  44. }
  45. if (!Directory.Exists(destination))
  46. Directory.CreateDirectory(destination);
  47. FileInfo[] files = dir.GetFiles();
  48. foreach (FileInfo file in files)
  49. {
  50. string temppath = Path.Combine(destination, file.Name);
  51. file.CopyTo(temppath, false);
  52. }
  53. foreach (DirectoryInfo subdir in dirs)
  54. {
  55. string temppath = Path.Combine(destination, subdir.Name);
  56. Copy(subdir.FullName, temppath);
  57. }
  58. }
  59. }
  60. /** @} */
  61. }