DirectoryEx.cs 2.6 KB

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