2
0

PathEx.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.IO;
  4. namespace BansheeEngine
  5. {
  6. /// <summary>
  7. /// Contains helper methods dealing with file and directory paths, extending the functionality provided by
  8. // System.IO.Path.
  9. /// </summary>
  10. public static class PathEx
  11. {
  12. /// <summary>
  13. /// Checks if two paths are equal.
  14. /// </summary>
  15. /// <param name="a">First path.</param>
  16. /// <param name="b">Second path.</param>
  17. /// <returns>True if both paths point to the same place.</returns>
  18. public static bool Compare(string a, string b)
  19. {
  20. return Path.GetFullPath(a) == Path.GetFullPath(b);
  21. }
  22. /// <summary>
  23. /// Checks if one path is part of an another path.
  24. /// </summary>
  25. /// <param name="path">Path to check if it's part of <paramref name="parent"/>.</param>
  26. /// <param name="parent">Parent that might contain <paramref name="path"/>.</param>
  27. /// <returns>True if <paramref name="parent"/> contains <paramref name="path"/>.</returns>
  28. public static bool IsPartOf(string path, string parent)
  29. {
  30. return Path.GetFullPath(path).StartsWith(Path.GetFullPath(parent));
  31. }
  32. /// <summary>
  33. /// Returns the last entry in the path, regardless if that is a directory or a filename.
  34. /// </summary>
  35. /// <param name="path">Path to get the tail of.</param>
  36. /// <returns>Tail of the path. This might be a directory or a filename.</returns>
  37. public static string GetTail(string path)
  38. {
  39. if (string.IsNullOrEmpty(path))
  40. return "";
  41. if (path[path.Length - 1] == Path.DirectorySeparatorChar ||
  42. path[path.Length - 1] == Path.AltDirectorySeparatorChar)
  43. {
  44. return Path.GetDirectoryName(path);
  45. }
  46. return Path.GetFileName(path);
  47. }
  48. /// <summary>
  49. /// Returns the parent of the provided path.
  50. /// </summary>
  51. /// <param name="path">Path to return the parent of.</param>
  52. /// <returns>Parent of the path. This usually means the folder containing the file or folder in the current path.
  53. /// </returns>
  54. public static string GetParent(string path)
  55. {
  56. string tail = GetTail(path);
  57. return path.Remove(path.Length - tail.Length);
  58. }
  59. /// <summary>
  60. /// Checks if the provided name can be used as a file name.
  61. /// </summary>
  62. /// <param name="name">Name to check.</param>
  63. /// <returns>True if the name can be used as a file name.</returns>
  64. public static bool IsValidFileName(string name)
  65. {
  66. return !string.IsNullOrWhiteSpace(name) &&
  67. name.IndexOfAny(Path.GetInvalidFileNameChars()) < 0;
  68. }
  69. }
  70. }