FileUtils.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.IO;
  2. using System.Linq;
  3. public static class FileUtils
  4. {
  5. /// <summary>
  6. /// 遍历文件夹中的文件【仅仅遍历当前目录】
  7. /// </summary>
  8. /// <param name="dir"></param>
  9. /// <param name="filter"></param>
  10. /// <param name="option"></param>
  11. /// <returns></returns>
  12. public static string[] TraverseFiles(string dir, string filter, string option = "")
  13. {
  14. if (string.IsNullOrEmpty(option))
  15. return Directory.GetFiles(dir, filter, SearchOption.TopDirectoryOnly);
  16. else
  17. return Directory.GetFiles(dir, filter, SearchOption.TopDirectoryOnly).Where(s => option.Contains(Path.GetExtension(s).ToLower())).ToArray();
  18. }
  19. /// <summary>
  20. /// 遍历文件夹中的文件
  21. /// </summary>
  22. /// <param name="dir">文件夹</param>
  23. /// <param name="filter">查找的文件后缀</param>
  24. /// <param name="option"></param>
  25. /// <returns></returns>
  26. public static string[] TraverseAllFiles(string dir, string filter, string option = "")
  27. {
  28. if (string.IsNullOrEmpty(option))
  29. return Directory.GetFiles(dir, filter, SearchOption.AllDirectories);
  30. else
  31. return Directory.GetFiles(dir, filter, SearchOption.AllDirectories).Where(s => option.Contains(Path.GetExtension(s).ToLower())).ToArray();
  32. }
  33. /// <summary>
  34. /// 获取资源相对路径
  35. /// </summary>
  36. /// <param name="filePath">资源路径</param>
  37. /// <returns>在Editor下的资源相对路径</returns>
  38. public static string ExtractAssetRelativePath(string filePath)
  39. {
  40. filePath = filePath.Replace('\\', '/');
  41. int pos = filePath.IndexOf("Assets");
  42. if (pos == -1)
  43. return string.Empty;
  44. string relativePath = filePath.Substring(pos, filePath.Length - pos);
  45. return relativePath;
  46. }
  47. /// <summary>
  48. /// 去掉文件后缀
  49. /// </summary>
  50. /// <param name="fileName">文件名字</param>
  51. /// <returns></returns>
  52. public static string RemoveExtension(string fileName)
  53. {
  54. int pos = fileName.LastIndexOf(".");
  55. if (pos == -1)
  56. return fileName;
  57. return fileName.Substring(0, pos);
  58. }
  59. /// <summary>
  60. /// 文件是否有后缀
  61. /// </summary>
  62. /// <param name="fileName"></param>
  63. /// <returns></returns>
  64. public static bool HasExtension(string fileName)
  65. {
  66. int pos = fileName.LastIndexOf(".");
  67. if (pos == -1)
  68. return false;
  69. return true;
  70. }
  71. /// <summary>
  72. /// 获取文件名字,去掉文件的目录
  73. /// </summary>
  74. /// <param name="filePath"></param>
  75. /// <returns></returns>
  76. public static string ExtractPureName(string filePath)
  77. {
  78. filePath = filePath.Replace('\\', '/');
  79. int pos = filePath.LastIndexOf("/");
  80. if (pos == -1)
  81. return filePath;
  82. return filePath.Substring(pos + 1, filePath.Length - pos - 1);
  83. }
  84. /// <summary>
  85. /// 获取文件所在目录
  86. /// </summary>
  87. /// <param name="filePath"></param>
  88. /// <returns></returns>
  89. public static bool ExtractParent(string filePath,out string dir)
  90. {
  91. dir = null;
  92. filePath = filePath.Replace('\\', '/');
  93. int pos = filePath.LastIndexOf("/");
  94. if (pos == -1)
  95. return false;
  96. dir = filePath.Substring(0, pos);
  97. return true;
  98. }
  99. public static string RemoveParent(string parent,string filePath)
  100. {
  101. filePath = filePath.Replace('\\', '/');
  102. int pos = filePath.IndexOf(parent);
  103. if (pos == -1)
  104. return filePath;
  105. string relativePath = filePath.Substring(parent.Length+1);
  106. return relativePath;
  107. }
  108. }