CSharpDirectoryOperator.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.IO;
  3. namespace XGame.Framework.FileSystem
  4. {
  5. class CSharpDirectoryOperator : IDirectoryOperator
  6. {
  7. public bool Copy(string source, string destination, bool overwrite)
  8. {
  9. if (Directory.Exists(destination))
  10. {
  11. if (overwrite)
  12. {
  13. Directory.Delete(destination, true);
  14. }
  15. }
  16. else
  17. {
  18. Directory.CreateDirectory(destination);
  19. }
  20. var files = Directory.GetFiles(source, "", SearchOption.AllDirectories);
  21. if (files == null || files.Length == 0)
  22. {
  23. return true;
  24. }
  25. foreach (var file in files)
  26. {
  27. var destFile = file.Replace(source, destination);
  28. var dirPath = Path.GetDirectoryName(file);
  29. if (!Directory.Exists(dirPath))
  30. {
  31. Directory.CreateDirectory(dirPath);
  32. }
  33. System.IO.File.Copy(file, destFile, overwrite);
  34. }
  35. return true;
  36. }
  37. public bool Delete(string fullDirectory, bool includeSelf = true)
  38. {
  39. Directory.Delete(fullDirectory, true);
  40. if (!includeSelf)
  41. Directory.CreateDirectory(fullDirectory);
  42. return true;
  43. }
  44. public IDeleteDirectoryAsync DeleteAsync(string fullDirectory, bool includeSelf = true)
  45. {
  46. DeleteDirectoryAsync deleteAsync = new DeleteDirectoryAsync(fullDirectory, includeSelf);
  47. deleteAsync.Start();
  48. return deleteAsync;
  49. }
  50. public bool Exist(string fullDirectory)
  51. {
  52. return Directory.Exists(fullDirectory);
  53. }
  54. public string GetDirectoryName(string fullDirectory)
  55. {
  56. return Path.GetDirectoryName(fullDirectory);
  57. }
  58. public string[] GetFileSystemEntries(string fullDirectory)
  59. {
  60. try
  61. {
  62. return Directory.GetFileSystemEntries(fullDirectory);
  63. }
  64. catch (Exception e)
  65. {
  66. UnityEngine.Debug.LogError(e.Message);
  67. return null;
  68. }
  69. }
  70. public bool Mkdir(string fullDirectory)
  71. {
  72. Directory.CreateDirectory(fullDirectory);
  73. return true;
  74. }
  75. public bool Rename(string from, string to)
  76. {
  77. Directory.Move(from, to);
  78. return true;
  79. }
  80. public string[] GetFiles(string fullDirectory, string extension, bool recursion = true)
  81. {
  82. return Directory.GetFiles(fullDirectory, extension, recursion ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
  83. }
  84. }
  85. }