Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

204 righe
8.9 KiB

  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.Diagnostics.CodeAnalysis;
  23. using System.IO;
  24. using System.Security;
  25. namespace Alphaleonis.Win32.Filesystem
  26. {
  27. /// <summary>Exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.</summary>
  28. [SerializableAttribute]
  29. public sealed partial class DirectoryInfo : FileSystemInfo
  30. {
  31. #region Constructors
  32. #region .NET
  33. /// <summary>Initializes a new instance of the <see cref="Alphaleonis.Win32.Filesystem.DirectoryInfo"/> class on the specified path.</summary>
  34. /// <param name="path">The path on which to create the <see cref="Alphaleonis.Win32.Filesystem.DirectoryInfo"/>.</param>
  35. /// <remarks>
  36. /// This constructor does not check if a directory exists. This constructor is a placeholder for a string that is used to access the disk in subsequent operations.
  37. /// The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.
  38. /// </remarks>
  39. public DirectoryInfo(string path) : this(null, path, PathFormat.RelativePath)
  40. {
  41. }
  42. #endregion // .NET
  43. #region AlphaFS
  44. /// <summary>[AlphaFS] Initializes a new instance of the <see cref="Alphaleonis.Win32.Filesystem.DirectoryInfo"/> class on the specified path.</summary>
  45. /// <param name="path">The path on which to create the <see cref="Alphaleonis.Win32.Filesystem.DirectoryInfo"/>.</param>
  46. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  47. /// <remarks>This constructor does not check if a directory exists. This constructor is a placeholder for a string that is used to access the disk in subsequent operations.</remarks>
  48. public DirectoryInfo(string path, PathFormat pathFormat) : this(null, path, pathFormat)
  49. {
  50. }
  51. /// <summary>[AlphaFS] Special internal implementation.</summary>
  52. /// <param name="transaction">The transaction.</param>
  53. /// <param name="fullPath">The full path on which to create the <see cref="Alphaleonis.Win32.Filesystem.DirectoryInfo"/>.</param>
  54. /// <param name="junk1">Not used.</param>
  55. /// <param name="junk2">Not used.</param>
  56. /// <remarks>This constructor does not check if a directory exists. This constructor is a placeholder for a string that is used to access the disk in subsequent operations.</remarks>
  57. [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "junk1")]
  58. [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "junk2")]
  59. private DirectoryInfo(KernelTransaction transaction, string fullPath, bool junk1, bool junk2)
  60. {
  61. IsDirectory = true;
  62. Transaction = transaction;
  63. LongFullName = Path.GetLongPathCore(fullPath, GetFullPathOptions.None);
  64. OriginalPath = Path.GetFileName(fullPath, true);
  65. FullPath = fullPath;
  66. DisplayPath = OriginalPath.Length != 2 || OriginalPath[1] != Path.VolumeSeparatorChar ? OriginalPath : Path.CurrentDirectoryPrefix;
  67. }
  68. #region Transactional
  69. /// <summary>[AlphaFS] Initializes a new instance of the <see cref="Alphaleonis.Win32.Filesystem.DirectoryInfo"/> class on the specified path.</summary>
  70. /// <param name="transaction">The transaction.</param>
  71. /// <param name="path">The path on which to create the <see cref="Alphaleonis.Win32.Filesystem.DirectoryInfo"/>.</param>
  72. /// <remarks>This constructor does not check if a directory exists. This constructor is a placeholder for a string that is used to access the disk in subsequent operations.</remarks>
  73. public DirectoryInfo(KernelTransaction transaction, string path) : this(transaction, path, PathFormat.RelativePath)
  74. {
  75. }
  76. /// <summary>[AlphaFS] Initializes a new instance of the <see cref="Alphaleonis.Win32.Filesystem.DirectoryInfo"/> class on the specified path.</summary>
  77. /// <param name="transaction">The transaction.</param>
  78. /// <param name="path">The path on which to create the <see cref="Alphaleonis.Win32.Filesystem.DirectoryInfo"/>.</param>
  79. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  80. /// <remarks>This constructor does not check if a directory exists. This constructor is a placeholder for a string that is used to access the disk in subsequent operations.</remarks>
  81. public DirectoryInfo(KernelTransaction transaction, string path, PathFormat pathFormat)
  82. {
  83. InitializeCore(true, transaction, path, pathFormat);
  84. }
  85. #endregion // Transactional
  86. #endregion // AlphaFS
  87. #endregion // Constructors
  88. #region Properties
  89. #region .NET
  90. #region Exists
  91. /// <summary>Gets a value indicating whether the directory exists.</summary>
  92. /// <remarks>
  93. /// <para>The <see cref="Exists"/> property returns <see langword="false"/> if any error occurs while trying to determine if the
  94. /// specified directory exists.</para>
  95. /// <para>This can occur in situations that raise exceptions such as passing a directory name with invalid characters or too many
  96. /// characters,</para>
  97. /// <para>a failing or missing disk, or if the caller does not have permission to read the directory.</para>
  98. /// </remarks>
  99. /// <value><see langword="true"/> if the directory exists; otherwise, <see langword="false"/>.</value>
  100. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  101. public override bool Exists
  102. {
  103. [SecurityCritical]
  104. get
  105. {
  106. try
  107. {
  108. if (DataInitialised == -1)
  109. Refresh();
  110. FileAttributes attrs = Win32AttributeData.dwFileAttributes;
  111. return DataInitialised == 0 && attrs != (FileAttributes) (-1) && (attrs & FileAttributes.Directory) != 0;
  112. }
  113. catch
  114. {
  115. return false;
  116. }
  117. }
  118. }
  119. #endregion // Exists
  120. #region Name
  121. /// <summary>Gets the name of this <see cref="DirectoryInfo"/> instance.</summary>
  122. /// <value>The directory name.</value>
  123. /// <remarks>
  124. /// <para>This Name property returns only the name of the directory, such as "Bin".</para>
  125. /// <para>To get the full path, such as "c:\public\Bin", use the FullName property.</para>
  126. /// </remarks>
  127. public override string Name
  128. {
  129. get
  130. {
  131. // GetDirName()
  132. return FullPath.Length > 3
  133. ? Path.GetFileName(Path.RemoveTrailingDirectorySeparator(FullPath, false), true)
  134. : FullPath;
  135. }
  136. }
  137. #endregion // Name
  138. #region Parent
  139. /// <summary>Gets the parent directory of a specified subdirectory.</summary>
  140. /// <value>The parent directory, or null if the path is null or if the file path denotes a root (such as "\", "C:", or * "\\server\share").</value>
  141. public DirectoryInfo Parent
  142. {
  143. [SecurityCritical]
  144. get
  145. {
  146. string path = FullPath;
  147. if (path.Length > 3)
  148. path = Path.RemoveTrailingDirectorySeparator(FullPath, false);
  149. string dirName = Path.GetDirectoryName(path, false);
  150. return dirName == null ? null : new DirectoryInfo(Transaction, dirName, true, true);
  151. }
  152. }
  153. #endregion // Parent
  154. #region Root
  155. /// <summary>Gets the root portion of the directory.</summary>
  156. /// <value>An object that represents the root of the directory.</value>
  157. public DirectoryInfo Root
  158. {
  159. [SecurityCritical]
  160. get { return new DirectoryInfo(Transaction, Path.GetPathRoot(FullPath, false), PathFormat.RelativePath); }
  161. }
  162. #endregion // Root
  163. #endregion // .NET
  164. #endregion // Properties
  165. }
  166. }