25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

212 satır
12 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.Globalization;
  23. using System.IO;
  24. using System.Linq;
  25. using System.Security;
  26. namespace Alphaleonis.Win32.Filesystem
  27. {
  28. partial class Directory
  29. {
  30. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  31. /// <exception cref="ArgumentException"/>
  32. /// <exception cref="ArgumentNullException"/>
  33. /// <exception cref="ArgumentNullException"/>
  34. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  35. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  36. [SecurityCritical]
  37. public static void DeleteEmptySubdirectories(string path, bool recursive)
  38. {
  39. DeleteEmptySubdirectoriesCore(null, null, path, recursive, false, true, PathFormat.RelativePath);
  40. }
  41. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  42. /// <exception cref="ArgumentException"/>
  43. /// <exception cref="ArgumentNullException"/>
  44. /// <exception cref="ArgumentNullException"/>
  45. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  46. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  47. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  48. [SecurityCritical]
  49. public static void DeleteEmptySubdirectories(string path, bool recursive, PathFormat pathFormat)
  50. {
  51. DeleteEmptySubdirectoriesCore(null, null, path, recursive, false, true, pathFormat);
  52. }
  53. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  54. /// <exception cref="ArgumentException"/>
  55. /// <exception cref="ArgumentNullException"/>
  56. /// <exception cref="ArgumentNullException"/>
  57. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  58. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  59. /// <param name="ignoreReadOnly"><see langword="true"/> overrides read only <see cref="FileAttributes"/> of empty directories.</param>
  60. [SecurityCritical]
  61. public static void DeleteEmptySubdirectories(string path, bool recursive, bool ignoreReadOnly)
  62. {
  63. DeleteEmptySubdirectoriesCore(null, null, path, recursive, ignoreReadOnly, true, PathFormat.RelativePath);
  64. }
  65. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  66. /// <exception cref="ArgumentException"/>
  67. /// <exception cref="ArgumentNullException"/>
  68. /// <exception cref="ArgumentNullException"/>
  69. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  70. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  71. /// <param name="ignoreReadOnly"><see langword="true"/> overrides read only <see cref="FileAttributes"/> of empty directories.</param>
  72. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  73. [SecurityCritical]
  74. public static void DeleteEmptySubdirectories(string path, bool recursive, bool ignoreReadOnly, PathFormat pathFormat)
  75. {
  76. DeleteEmptySubdirectoriesCore(null, null, path, recursive, ignoreReadOnly, true, pathFormat);
  77. }
  78. #region Transactional
  79. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  80. /// <exception cref="ArgumentException"/>
  81. /// <exception cref="ArgumentNullException"/>
  82. /// <exception cref="ArgumentNullException"/>
  83. /// <param name="transaction">The transaction.</param>
  84. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  85. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  86. [SecurityCritical]
  87. public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path, bool recursive)
  88. {
  89. DeleteEmptySubdirectoriesCore(null, transaction, path, recursive, false, true, PathFormat.RelativePath);
  90. }
  91. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  92. /// <exception cref="ArgumentException"/>
  93. /// <exception cref="ArgumentNullException"/>
  94. /// <exception cref="ArgumentNullException"/>
  95. /// <param name="transaction">The transaction.</param>
  96. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  97. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  98. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  99. [SecurityCritical]
  100. public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path, bool recursive, PathFormat pathFormat)
  101. {
  102. DeleteEmptySubdirectoriesCore(null, transaction, path, recursive, false, true, pathFormat);
  103. }
  104. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  105. /// <exception cref="ArgumentException"/>
  106. /// <exception cref="ArgumentNullException"/>
  107. /// <exception cref="ArgumentNullException"/>
  108. /// <param name="transaction">The transaction.</param>
  109. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  110. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  111. /// <param name="ignoreReadOnly"><see langword="true"/> overrides read only <see cref="FileAttributes"/> of empty directories.</param>
  112. [SecurityCritical]
  113. public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly)
  114. {
  115. DeleteEmptySubdirectoriesCore(null, transaction, path, recursive, ignoreReadOnly, true, PathFormat.RelativePath);
  116. }
  117. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  118. /// <exception cref="ArgumentException"/>
  119. /// <exception cref="ArgumentNullException"/>
  120. /// <exception cref="ArgumentNullException"/>
  121. /// <param name="transaction">The transaction.</param>
  122. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  123. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  124. /// <param name="ignoreReadOnly"><see langword="true"/> overrides read only <see cref="FileAttributes"/> of empty directories.</param>
  125. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  126. [SecurityCritical]
  127. public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly, PathFormat pathFormat)
  128. {
  129. DeleteEmptySubdirectoriesCore(null, transaction, path, recursive, ignoreReadOnly, true, pathFormat);
  130. }
  131. #endregion // Transactional
  132. #region Internal Methods
  133. /// <summary>Delete empty subdirectories from the specified directory.</summary>
  134. /// <exception cref="ArgumentException"/>
  135. /// <exception cref="ArgumentNullException"/>
  136. /// <exception cref="DirectoryNotFoundException"/>
  137. /// <exception cref="IOException"/>
  138. /// <exception cref="NotSupportedException"/>
  139. /// <exception cref="UnauthorizedAccessException"/>
  140. /// <param name="fileSystemEntryInfo">A FileSystemEntryInfo instance. Use either <paramref name="fileSystemEntryInfo"/> or <paramref name="path"/>, not both.</param>
  141. /// <param name="transaction">The transaction.</param>
  142. /// <param name="path">The name of the directory to remove empty subdirectories from. Use either <paramref name="path"/> or <paramref name="fileSystemEntryInfo"/>, not both.</param>
  143. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  144. /// <param name="ignoreReadOnly"><see langword="true"/> overrides read only <see cref="FileAttributes"/> of empty directories.</param>
  145. /// <param name="initialize">When <see langword="true"/> indicates the method is called externally.</param>
  146. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  147. [SecurityCritical]
  148. internal static void DeleteEmptySubdirectoriesCore(FileSystemEntryInfo fileSystemEntryInfo, KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly, bool initialize, PathFormat pathFormat)
  149. {
  150. #region Setup
  151. if (pathFormat == PathFormat.RelativePath)
  152. Path.CheckSupportedPathFormat(path, true, true);
  153. if (fileSystemEntryInfo == null)
  154. {
  155. if (!File.ExistsCore(true, transaction, path, pathFormat))
  156. NativeError.ThrowException(Win32Errors.ERROR_PATH_NOT_FOUND, path);
  157. fileSystemEntryInfo = File.GetFileSystemEntryInfoCore(true, transaction, Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck) , false, pathFormat);
  158. }
  159. if (fileSystemEntryInfo == null)
  160. throw new ArgumentNullException("path");
  161. string pathLp = fileSystemEntryInfo.LongFullPath;
  162. #endregion // Setup
  163. // Ensure path is a directory.
  164. if (!fileSystemEntryInfo.IsDirectory)
  165. throw new IOException(string.Format(CultureInfo.CurrentCulture, Resources.Target_Directory_Is_A_File, pathLp));
  166. var dirEnumOptions = DirectoryEnumerationOptions.Folders;
  167. if (recursive)
  168. dirEnumOptions |= DirectoryEnumerationOptions.Recursive;
  169. foreach (var fsei in EnumerateFileSystemEntryInfosCore<FileSystemEntryInfo>(transaction, pathLp, Path.WildcardStarMatchAll, dirEnumOptions, PathFormat.LongFullPath))
  170. DeleteEmptySubdirectoriesCore(fsei, transaction, null, recursive, ignoreReadOnly, false, PathFormat.LongFullPath);
  171. if (!EnumerateFileSystemEntryInfosCore<string>(transaction, pathLp, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.LongFullPath).Any())
  172. {
  173. // Prevent deleting path itself.
  174. if (!initialize)
  175. DeleteDirectoryCore(fileSystemEntryInfo, transaction, null, false, ignoreReadOnly, true, true, PathFormat.LongFullPath);
  176. }
  177. }
  178. #endregion // Internal Methods
  179. }
  180. }