You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Directory.GetDirectoryRoot.cs 5.9 KiB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.Security;
  23. namespace Alphaleonis.Win32.Filesystem
  24. {
  25. partial class Directory
  26. {
  27. #region .NET
  28. /// <summary>Returns the volume information, root information, or both for the specified path.</summary>
  29. /// <returns>The volume information, root information, or both for the specified path, or <see langword="null"/> if <paramref name="path"/> path does not contain root directory information.</returns>
  30. /// <exception cref="ArgumentException"/>
  31. /// <exception cref="ArgumentNullException"/>
  32. /// <exception cref="NotSupportedException"/>
  33. /// <param name="path">The path of a file or directory.</param>
  34. [SecurityCritical]
  35. public static string GetDirectoryRoot(string path)
  36. {
  37. return GetDirectoryRootCore(null, path, PathFormat.RelativePath);
  38. }
  39. #endregion // .NET
  40. /// <summary>Returns the volume information, root information, or both for the specified path.</summary>
  41. /// <returns>The volume information, root information, or both for the specified path, or <see langword="null"/> if <paramref name="path"/> path does not contain root directory information.</returns>
  42. /// <exception cref="ArgumentException"/>
  43. /// <exception cref="ArgumentNullException"/>
  44. /// <exception cref="NotSupportedException"/>
  45. /// <param name="path">The path of a file or directory.</param>
  46. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  47. [SecurityCritical]
  48. public static string GetDirectoryRoot(string path, PathFormat pathFormat)
  49. {
  50. return GetDirectoryRootCore(null, path, pathFormat);
  51. }
  52. #region Transactional
  53. /// <summary>[AlphaFS] Returns the volume information, root information, or both for the specified path.</summary>
  54. /// <returns>The volume information, root information, or both for the specified path, or <see langword="null"/> if <paramref name="path"/> path does not contain root directory information.</returns>
  55. /// <exception cref="ArgumentException"/>
  56. /// <exception cref="ArgumentNullException"/>
  57. /// <exception cref="NotSupportedException"/>
  58. /// <param name="transaction">The transaction.</param>
  59. /// <param name="path">The path of a file or directory.</param>
  60. [SecurityCritical]
  61. public static string GetDirectoryRootTransacted(KernelTransaction transaction, string path)
  62. {
  63. return GetDirectoryRootCore(transaction, path, PathFormat.RelativePath);
  64. }
  65. /// <summary>Returns the volume information, root information, or both for the specified path.</summary>
  66. /// <returns>The volume information, root information, or both for the specified path, or <see langword="null"/> if <paramref name="path"/> path does not contain root directory information.</returns>
  67. /// <exception cref="ArgumentException"/>
  68. /// <exception cref="ArgumentNullException"/>
  69. /// <exception cref="NotSupportedException"/>
  70. /// <param name="transaction">The transaction.</param>
  71. /// <param name="path">The path of a file or directory.</param>
  72. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  73. [SecurityCritical]
  74. public static string GetDirectoryRootTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  75. {
  76. return GetDirectoryRootCore(transaction, path, pathFormat);
  77. }
  78. #endregion // Transactional
  79. #region Internal Methods
  80. /// <summary>Returns the volume information, root information, or both for the specified path.</summary>
  81. /// <returns>The volume information, root information, or both for the specified path, or <see langword="null"/> if <paramref name="path"/> path does not contain root directory information.</returns>
  82. /// <exception cref="ArgumentException"/>
  83. /// <exception cref="ArgumentNullException"/>
  84. /// <exception cref="NotSupportedException"/>
  85. /// <param name="transaction">The transaction.</param>
  86. /// <param name="path">The path of a file or directory.</param>
  87. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  88. [SecurityCritical]
  89. internal static string GetDirectoryRootCore(KernelTransaction transaction, string path, PathFormat pathFormat)
  90. {
  91. Path.CheckInvalidUncPath(path);
  92. string pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.CheckInvalidPathChars);
  93. pathLp = Path.GetRegularPathCore(pathLp, GetFullPathOptions.None, false);
  94. string rootPath = Path.GetPathRoot(pathLp, false);
  95. return Utils.IsNullOrWhiteSpace(rootPath) ? null : rootPath;
  96. }
  97. #endregion // Internal Methods
  98. }
  99. }