FileHelp.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using QM.KJGH.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using WS.IO;
  8. namespace QM.KJGH.CGGL
  9. {
  10. public static class FileHelp
  11. {
  12. #region
  13. /// <summary>
  14. /// 获取目录文件
  15. /// </summary>
  16. /// <param name="rootpath">根目录</param>
  17. /// <returns></returns>
  18. public static List<Tree> FileCatalog(string rootpath) {
  19. List<Tree> trees = new List<Tree>();
  20. if (!Directory.Exists(rootpath))
  21. return trees;
  22. ForDirectory(rootpath, trees);
  23. return trees;
  24. }
  25. private static void ForDirectory(string path, List<Tree> trees)
  26. {
  27. var dirs = Directory.GetDirectories(path);
  28. foreach (var item in dirs)
  29. {
  30. var ptree = new Tree
  31. {
  32. label = Path.GetFileName(item),
  33. value = Path.GetFileName(item),
  34. };
  35. if (item.EndsWith(".gdb"))
  36. {
  37. ptree.IsParent = false;
  38. ptree.Add = FileManage.Instance.ToRelativePath(item);
  39. }
  40. else
  41. {
  42. ptree.IsParent = true;
  43. ptree.children = new List<Tree>();
  44. ForDirectory(item, ptree.children);
  45. }
  46. trees.Add(ptree);
  47. }
  48. trees.AddRange(Directory.GetFiles(path).Select(f => {
  49. return new Tree
  50. {
  51. label = Path.GetFileName(f),
  52. value = Path.GetFileName(f),
  53. IsParent = false,
  54. Add = FileManage.Instance.ToRelativePath(f)
  55. };
  56. }));
  57. }
  58. #endregion
  59. }
  60. }