2
0

DirectoryEx.cs 2.2 KB

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