WindowsPathUtils.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. namespace ICSharpCode.SharpZipLib.Core
  2. {
  3. /// <summary>
  4. /// WindowsPathUtils provides simple utilities for handling windows paths.
  5. /// </summary>
  6. public abstract class WindowsPathUtils
  7. {
  8. /// <summary>
  9. /// Initializes a new instance of the <see cref="WindowsPathUtils"/> class.
  10. /// </summary>
  11. internal WindowsPathUtils()
  12. {
  13. }
  14. /// <summary>
  15. /// Remove any path root present in the path
  16. /// </summary>
  17. /// <param name="path">A <see cref="string"/> containing path information.</param>
  18. /// <returns>The path with the root removed if it was present; path otherwise.</returns>
  19. /// <remarks>Unlike the <see cref="System.IO.Path"/> class the path isnt otherwise checked for validity.</remarks>
  20. public static string DropPathRoot(string path)
  21. {
  22. string result = path;
  23. if (!string.IsNullOrEmpty(path))
  24. {
  25. if ((path[0] == '\\') || (path[0] == '/'))
  26. {
  27. // UNC name ?
  28. if ((path.Length > 1) && ((path[1] == '\\') || (path[1] == '/')))
  29. {
  30. int index = 2;
  31. int elements = 2;
  32. // Scan for two separate elements \\machine\share\restofpath
  33. while ((index <= path.Length) &&
  34. (((path[index] != '\\') && (path[index] != '/')) || (--elements > 0)))
  35. {
  36. index++;
  37. }
  38. index++;
  39. if (index < path.Length)
  40. {
  41. result = path.Substring(index);
  42. }
  43. else
  44. {
  45. result = "";
  46. }
  47. }
  48. }
  49. else if ((path.Length > 1) && (path[1] == ':'))
  50. {
  51. int dropCount = 2;
  52. if ((path.Length > 2) && ((path[2] == '\\') || (path[2] == '/')))
  53. {
  54. dropCount = 3;
  55. }
  56. result = result.Remove(0, dropCount);
  57. }
  58. }
  59. return result;
  60. }
  61. }
  62. }