Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Directory.GetParent.cs 5.1 KiB

pirms 7 gadiem
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.Security;
  22. namespace Alphaleonis.Win32.Filesystem
  23. {
  24. partial class Directory
  25. {
  26. #region .NET
  27. /// <summary>Retrieves the parent directory of the specified path, including both absolute and relative paths.</summary>
  28. /// <param name="path">The path for which to retrieve the parent directory.</param>
  29. /// <returns>The parent directory, or <see langword="null"/> if <paramref name="path"/> is the root directory, including the root of a UNC server or share name.</returns>
  30. [SecurityCritical]
  31. public static DirectoryInfo GetParent(string path)
  32. {
  33. return GetParentCore(null, path, PathFormat.RelativePath);
  34. }
  35. /// <summary>[AlphaFS] Retrieves the parent directory of the specified path, including both absolute and relative paths.</summary>
  36. /// <returns>The parent directory, or <see langword="null"/> if <paramref name="path"/> is the root directory, including the root of a UNC server or share name.</returns>
  37. /// <param name="path">The path for which to retrieve the parent directory.</param>
  38. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  39. [SecurityCritical]
  40. public static DirectoryInfo GetParent(string path, PathFormat pathFormat)
  41. {
  42. return GetParentCore(null, path, pathFormat);
  43. }
  44. #endregion // .NET
  45. /// <summary>[AlphaFS] Retrieves the parent directory of the specified path, including both absolute and relative paths.</summary>
  46. /// <returns>The parent directory, or <see langword="null"/> if <paramref name="path"/> is the root directory, including the root of a UNC server or share name.</returns>
  47. /// <param name="transaction">The transaction.</param>
  48. /// <param name="path">The path for which to retrieve the parent directory.</param>
  49. [SecurityCritical]
  50. public static DirectoryInfo GetParentTransacted(KernelTransaction transaction, string path)
  51. {
  52. return GetParentCore(transaction, path, PathFormat.RelativePath);
  53. }
  54. /// <summary>Retrieves the parent directory of the specified path, including both absolute and relative paths.</summary>
  55. /// <returns>The parent directory, or <see langword="null"/> if <paramref name="path"/> is the root directory, including the root of a UNC server or share name.</returns>
  56. /// <param name="transaction">The transaction.</param>
  57. /// <param name="path">The path for which to retrieve the parent directory.</param>
  58. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  59. [SecurityCritical]
  60. public static DirectoryInfo GetParentTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  61. {
  62. return GetParentCore(transaction, path, pathFormat);
  63. }
  64. #region Internal Methods
  65. /// <summary>Retrieves the parent directory of the specified path, including both absolute and relative paths.</summary>
  66. /// <returns>The parent directory, or <see langword="null"/> if <paramref name="path"/> is the root directory, including the root of a UNC server or share name.</returns>
  67. /// <param name="transaction">The transaction.</param>
  68. /// <param name="path">The path for which to retrieve the parent directory.</param>
  69. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  70. [SecurityCritical]
  71. internal static DirectoryInfo GetParentCore(KernelTransaction transaction, string path, PathFormat pathFormat)
  72. {
  73. string pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.CheckInvalidPathChars);
  74. pathLp = Path.GetRegularPathCore(pathLp, GetFullPathOptions.None, false);
  75. string dirName = Path.GetDirectoryName(pathLp, false);
  76. return Utils.IsNullOrWhiteSpace(dirName) ? null : new DirectoryInfo(transaction, dirName, PathFormat.RelativePath);
  77. }
  78. #endregion // Internal Methods
  79. }
  80. }