FileTool.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. }
  65. catch { }
  66. return false;
  67. }
  68. public static bool Delete(string[] files)
  69. {
  70. bool result = true;
  71. if (!ListTool.IsNullOrEmpty(files))
  72. {
  73. foreach (var file in files)
  74. {
  75. result = result || Delete(file);
  76. }
  77. }
  78. return result;
  79. }
  80. }
  81. }