2
0

PathEx.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if (string.IsNullOrEmpty(a))
  24. {
  25. if (string.IsNullOrEmpty(b))
  26. return true;
  27. return false;
  28. }
  29. else
  30. {
  31. if (string.IsNullOrEmpty(b))
  32. return false;
  33. }
  34. return Path.GetFullPath(a) == Path.GetFullPath(b);
  35. }
  36. /// <summary>
  37. /// Checks if one path is part of an another path.
  38. /// </summary>
  39. /// <param name="path">Path to check if it's part of <paramref name="parent"/>.</param>
  40. /// <param name="parent">Parent that might contain <paramref name="path"/>.</param>
  41. /// <returns>True if <paramref name="parent"/> contains <paramref name="path"/>.</returns>
  42. public static bool IsPartOf(string path, string parent)
  43. {
  44. return Path.GetFullPath(path).StartsWith(Path.GetFullPath(parent));
  45. }
  46. /// <summary>
  47. /// Returns the last entry in the path, regardless if that is a directory or a filename.
  48. /// </summary>
  49. /// <param name="path">Path to get the tail of.</param>
  50. /// <returns>Tail of the path. This might be a directory or a filename.</returns>
  51. public static string GetTail(string path)
  52. {
  53. if (string.IsNullOrEmpty(path))
  54. return "";
  55. if (path[path.Length - 1] == Path.DirectorySeparatorChar ||
  56. path[path.Length - 1] == Path.AltDirectorySeparatorChar)
  57. {
  58. return Path.GetFileName(path.Substring(0, path.Length - 1));
  59. }
  60. return Path.GetFileName(path);
  61. }
  62. /// <summary>
  63. /// Returns the parent of the provided path.
  64. /// </summary>
  65. /// <param name="path">Path to return the parent of.</param>
  66. /// <returns>Parent of the path. This usually means the folder containing the file or folder in the current path.
  67. /// </returns>
  68. public static string GetParent(string path)
  69. {
  70. string tail = GetTail(path);
  71. return path.Remove(path.Length - tail.Length);
  72. }
  73. /// <summary>
  74. /// Checks if the provided name can be used as a file name.
  75. /// </summary>
  76. /// <param name="name">Name to check.</param>
  77. /// <returns>True if the name can be used as a file name.</returns>
  78. public static bool IsValidFileName(string name)
  79. {
  80. return !string.IsNullOrWhiteSpace(name) &&
  81. name.IndexOfAny(Path.GetInvalidFileNameChars()) < 0;
  82. }
  83. }
  84. /** @} */
  85. }