FileFinder.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Azylee.Core.DataUtils.CollectionUtils;
  2. using Azylee.Core.IOUtils.DirUtils;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. namespace Azylee.Core.IOUtils.FileUtils
  9. {
  10. /// <summary>
  11. /// 文件搜索
  12. /// </summary>
  13. public static class FileFinder
  14. {
  15. /// <summary>
  16. /// 获取文件(单层目录)
  17. /// </summary>
  18. /// <param name="path">路径</param>
  19. /// <param name="pattern">通配符</param>
  20. /// <returns></returns>
  21. public static List<string> GetFile(string path, string pattern = "*")
  22. {
  23. try
  24. {
  25. if (Directory.Exists(path))
  26. {
  27. List<string> result = Directory.EnumerateFiles(path, pattern).ToList();
  28. return result;
  29. }
  30. }
  31. catch (Exception e) { }
  32. return null;
  33. }
  34. /// <summary>
  35. /// 获取所有目录中的所有文件
  36. /// </summary>
  37. /// <param name="path"></param>
  38. /// <param name="action"></param>
  39. /// <param name="patterns"></param>
  40. public static void GetAllFile(string path, Action<List<string>> action, string[] patterns = null)
  41. {
  42. List<string> allpath = DirFinder.GetAllPath(path);
  43. if (allpath == null) allpath = new List<string>();
  44. allpath.Add(path);
  45. foreach (var p in allpath)
  46. {
  47. if (Ls.Ok(patterns))
  48. {
  49. foreach (var pattern in patterns)
  50. {
  51. List<string> files = GetFile(p, pattern);
  52. if (Ls.Ok(files)) action?.Invoke(files);
  53. }
  54. }
  55. else
  56. {
  57. List<string> files = GetFile(p);
  58. if (Ls.Ok(files)) action?.Invoke(files);
  59. }
  60. }
  61. }
  62. }
  63. }