PathExtension.cs 1.5 KB

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