FileHelper.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Oreo.FileMan.DatabaseEngine;
  2. using Oreo.FileMan.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Y.Utils.DataUtils.Collections;
  10. using Y.Utils.IOUtils.FileUtils;
  11. using Y.Utils.IOUtils.PathUtils;
  12. namespace Oreo.FileMan.Helpers
  13. {
  14. public class FileHelper
  15. {
  16. public static void GetAllFileToDb()
  17. {
  18. DriveInfo[] allDirves = DriveInfo.GetDrives();
  19. if (ListTool.HasElements(allDirves))
  20. {
  21. foreach (var item in allDirves)
  22. {
  23. if (item.IsReady)
  24. {
  25. using (var db = new Muse())
  26. {
  27. List<string> paths = DirTool.GetAllPath(item.Name);
  28. if (ListTool.HasElements(paths))
  29. {
  30. paths.ForEach(path =>
  31. {
  32. List<string> files = FileTool.GetFile(path);
  33. if (ListTool.HasElements(files))
  34. {
  35. files.ForEach(file =>
  36. {
  37. if (!db.Set<Files>().Any(x => x.FullPath == file))
  38. {
  39. var a = db.Set<Files>().Add(
  40. new Files()
  41. {
  42. FullPath = file,
  43. FileName = Path.GetFileName(file),
  44. ExtName = Path.GetExtension(file),
  45. Size = FileTool.Size(file),
  46. });
  47. }
  48. });
  49. int count = db.SaveChanges();
  50. }
  51. });
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }