PathExtension.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. namespace assetBundleUtility
  2. {
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. public static class PathExtension
  7. {
  8. public static string GetRelativePath(this string path)
  9. {
  10. return "Assets" + path.Replace(Application.dataPath, "");
  11. }
  12. public static List<string> GetAllFileNameFromPath(this List<string> pathes)
  13. {
  14. List<string> fileName = new List<string>();
  15. foreach (var path in pathes)
  16. {
  17. fileName.Add(Path.GetFileName(path));
  18. }
  19. return fileName;
  20. }
  21. public static string GetUnRepeatFileName(this string fileName)
  22. {
  23. string extension = Path.GetExtension(fileName);
  24. string directory = Path.GetDirectoryName(fileName);
  25. string name = Path.GetFileNameWithoutExtension(fileName);
  26. int count = 1;
  27. string newFileName = $"{directory}{Path.DirectorySeparatorChar}{name}{extension}";
  28. while (File.Exists(newFileName))
  29. {
  30. newFileName = $"{directory}{Path.DirectorySeparatorChar}{name} ({count++}){extension}";
  31. }
  32. return newFileName;
  33. }
  34. public static string GetUnRepeatDirectoryName(this string directoryName)
  35. {
  36. int count = 1;
  37. string newDirectoryName = directoryName;
  38. while (Directory.Exists(newDirectoryName))
  39. {
  40. newDirectoryName = $"{directoryName} ({count++})";
  41. }
  42. return newDirectoryName;
  43. }
  44. }
  45. }