DirectoryEx.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.IO;
  2. namespace BansheeEngine
  3. {
  4. public static class DirectoryEx
  5. {
  6. public static void Move(string source, string destination)
  7. {
  8. string destParent = PathEx.GetParent(destination);
  9. if (!string.IsNullOrEmpty(destParent))
  10. {
  11. if (!Directory.Exists(destParent))
  12. Directory.CreateDirectory(destParent);
  13. }
  14. Directory.Move(source, destination);
  15. }
  16. public static void Copy(string source, string destination)
  17. {
  18. DirectoryInfo dir = new DirectoryInfo(source);
  19. DirectoryInfo[] dirs = dir.GetDirectories();
  20. if (!dir.Exists)
  21. {
  22. throw new DirectoryNotFoundException(
  23. "Source directory does not exist or could not be found: "
  24. + source);
  25. }
  26. if (!Directory.Exists(destination))
  27. Directory.CreateDirectory(destination);
  28. FileInfo[] files = dir.GetFiles();
  29. foreach (FileInfo file in files)
  30. {
  31. string temppath = Path.Combine(destination, file.Name);
  32. file.CopyTo(temppath, false);
  33. }
  34. foreach (DirectoryInfo subdir in dirs)
  35. {
  36. string temppath = Path.Combine(destination, subdir.Name);
  37. Copy(subdir.FullName, temppath);
  38. }
  39. }
  40. }
  41. }