WindowsPathUtils.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. if ((path[0] == '\\') || (path[0] == '/')) {
  25. // UNC name ?
  26. if ((path.Length > 1) && ((path[1] == '\\') || (path[1] == '/'))) {
  27. int index = 2;
  28. int elements = 2;
  29. // Scan for two separate elements \\machine\share\restofpath
  30. while ((index <= path.Length) &&
  31. (((path[index] != '\\') && (path[index] != '/')) || (--elements > 0))) {
  32. index++;
  33. }
  34. index++;
  35. if (index < path.Length) {
  36. result = path.Substring(index);
  37. } else {
  38. result = "";
  39. }
  40. }
  41. } else if ((path.Length > 1) && (path[1] == ':')) {
  42. int dropCount = 2;
  43. if ((path.Length > 2) && ((path[2] == '\\') || (path[2] == '/'))) {
  44. dropCount = 3;
  45. }
  46. result = result.Remove(0, dropCount);
  47. }
  48. }
  49. return result;
  50. }
  51. }
  52. }