FileHelper.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace GFGEditor
  5. {
  6. public class FileHelper
  7. {
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. /// <param name="dirPath"></param>
  12. /// <param name="excludeDirs"></param>,路径比须从Asset起,例如"Assets/Res/Effect"
  13. /// <param name="action"></param>
  14. public static void ForeachFileInDir(string dirPath, List<string> excludeDirs, Action<string> action)
  15. {
  16. List<string> excludeDirPaths = new List<string>();
  17. if (excludeDirs != null)
  18. {
  19. foreach (var excDir in excludeDirs)
  20. {
  21. var t = Environment.CurrentDirectory + "/" + excDir;
  22. t = t.Replace('\\', '/');
  23. excludeDirPaths.Add(t);
  24. }
  25. }
  26. var t1 = dirPath.Replace('\\', '/');
  27. if (Directory.Exists(dirPath) && !excludeDirPaths.Contains(t1))
  28. {
  29. var files = Directory.GetFiles(dirPath);
  30. var dirs = Directory.GetDirectories(dirPath);
  31. foreach (var file in files)
  32. {
  33. action?.Invoke(file);
  34. }
  35. foreach (var dir in dirs)
  36. {
  37. ForeachFileInDir(dir, excludeDirs, action);
  38. }
  39. }
  40. }
  41. }
  42. }