PathEx.cs 3.1 KB

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