FileTool.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Y.Utils.BaseUtils;
  6. namespace Y.Utils.FileUtils
  7. {
  8. public class FileTool
  9. {
  10. public static List<string> GetFile(string path)
  11. {
  12. if (Directory.Exists(path))
  13. try { return Directory.EnumerateFiles(path).ToList(); } catch (Exception e) { }
  14. return null;
  15. }
  16. public static List<string> GetFile(string path, string ext)
  17. {
  18. if (Directory.Exists(path))
  19. try
  20. {
  21. List<string> result = null;
  22. List<string> temp = Directory.EnumerateFiles(path).ToList();
  23. if (!ListTool.IsNullOrEmpty(temp))
  24. foreach (var item in temp)
  25. {
  26. if (Path.GetExtension(item).ToUpper() == ext.ToUpper())
  27. {
  28. if (result == null)
  29. result = new List<string>();
  30. result.Add(item);
  31. }
  32. }
  33. return result;
  34. }
  35. catch (Exception e) { }
  36. return null;
  37. }
  38. public static List<string> GetAllFile(string path)
  39. {
  40. List<string> pathList = DirTool.GetAllPath(path);
  41. List<string> result = GetFile(path);
  42. if (!ListTool.IsNullOrEmpty(pathList))
  43. {
  44. foreach (var item in pathList)
  45. {
  46. List<string> temp = GetFile(item);
  47. if (!ListTool.IsNullOrEmpty(temp))
  48. result.AddRange(temp);
  49. }
  50. }
  51. if (!ListTool.IsNullOrEmpty(result))
  52. return result;
  53. return null;
  54. }
  55. public static bool Delete(string file)
  56. {
  57. try
  58. {
  59. if (File.Exists(file))
  60. {
  61. File.Delete(file);
  62. return true;
  63. }
  64. else
  65. {
  66. return true;
  67. }
  68. }
  69. catch { }
  70. return false;
  71. }
  72. public static bool Delete(string[] files)
  73. {
  74. bool result = true;
  75. if (!ListTool.IsNullOrEmpty(files))
  76. {
  77. foreach (var file in files)
  78. {
  79. result = result || Delete(file);
  80. }
  81. }
  82. return result;
  83. }
  84. }
  85. }