FileHelper.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.FileQueryEngine.QueryEngine;
  10. using Y.Utils.DataUtils.Collections;
  11. using Y.Utils.IOUtils.FileUtils;
  12. using Y.Utils.IOUtils.PathUtils;
  13. namespace Oreo.FileMan.Helpers
  14. {
  15. public class FileHelper
  16. {
  17. public static void GetAllFileToDb()
  18. {
  19. var drives = FileQueryEngine.GetReadyNtfsDrives();
  20. if (ListTool.HasElements(drives))
  21. {
  22. foreach (var drive in drives)
  23. {
  24. var allFiles = FileQueryEngine.GetAllFiles(drive);
  25. if (ListTool.HasElements(allFiles))
  26. {
  27. using (var db = new Muse())
  28. {
  29. int addcount = 0;
  30. foreach (var file in allFiles)
  31. {
  32. if (!db.Set<Files>().Any(x => x.FullPath == file.FullFileName))
  33. {
  34. var a = db.Set<Files>().Add(
  35. new Files()
  36. {
  37. FullPath = file.FullFileName,
  38. FileName = file.FileName,
  39. ExtName = Path.GetExtension(file.FullFileName),
  40. Size = FileTool.Size(file.FullFileName),
  41. });
  42. addcount++;
  43. }
  44. if (addcount >= 1000)
  45. {
  46. addcount = 0;
  47. db.SaveChanges();
  48. }
  49. }
  50. int count = db.SaveChanges();
  51. }
  52. }
  53. }
  54. }
  55. DriveInfo[] allDirves = DriveInfo.GetDrives();
  56. if (ListTool.HasElements(allDirves))
  57. {
  58. foreach (var item in allDirves)
  59. {
  60. if (item.IsReady)
  61. {
  62. using (var db = new Muse())
  63. {
  64. List<string> paths = DirTool.GetAllPath(item.Name);
  65. if (ListTool.HasElements(paths))
  66. {
  67. paths.ForEach(path =>
  68. {
  69. List<string> files = FileTool.GetFile(path);
  70. if (ListTool.HasElements(files))
  71. {
  72. files.ForEach(file =>
  73. {
  74. if (!db.Set<Files>().Any(x => x.FullPath == file))
  75. {
  76. var a = db.Set<Files>().Add(
  77. new Files()
  78. {
  79. FullPath = file,
  80. FileName = Path.GetFileName(file),
  81. ExtName = Path.GetExtension(file),
  82. Size = FileTool.Size(file),
  83. });
  84. }
  85. });
  86. int count = db.SaveChanges();
  87. }
  88. });
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }