ServerPathUtils.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using Utilities;
  11. namespace SMBLibrary.Server
  12. {
  13. public class ServerPathUtils
  14. {
  15. /// <param name="path">UNC path, e.g. '\\192.168.1.1\Shared'</param>
  16. /// <returns>e.g. \Shared</returns>
  17. public static string GetRelativeServerPath(string path)
  18. {
  19. if (path.StartsWith(@"\\"))
  20. {
  21. int index = path.IndexOf('\\', 2);
  22. if (index > 0)
  23. {
  24. return path.Substring(index);
  25. }
  26. else
  27. {
  28. return String.Empty;
  29. }
  30. }
  31. return path;
  32. }
  33. /// <param name="path">UNC path, e.g. '\\192.168.1.1\Shared\*'</param>
  34. /// <returns>e.g. \*</returns>
  35. public static string GetRelativeSharePath(string path)
  36. {
  37. string relativePath = GetRelativeServerPath(path);
  38. int index = relativePath.IndexOf('\\', 1);
  39. if (index > 0)
  40. {
  41. return path.Substring(index);
  42. }
  43. else
  44. {
  45. return @"\";
  46. }
  47. }
  48. public static string GetShareName(string path)
  49. {
  50. string relativePath = GetRelativeServerPath(path);
  51. if (relativePath.StartsWith(@"\"))
  52. {
  53. relativePath = relativePath.Substring(1);
  54. }
  55. int indexOfSeparator = relativePath.IndexOf(@"\");
  56. if (indexOfSeparator >= 0)
  57. {
  58. relativePath = relativePath.Substring(0, indexOfSeparator);
  59. }
  60. return relativePath;
  61. }
  62. }
  63. }