WindowsPathUtils.cs 1.6 KB

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