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.
 
 

196 lines
11 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.IO;
  23. using System.Linq;
  24. using System.Security;
  25. namespace Alphaleonis.Win32.Filesystem
  26. {
  27. partial class Directory
  28. {
  29. /// <summary>[AlphaFS] Counts file system objects: files, folders or both) in a given directory.</summary>
  30. /// <returns>The counted number of file system objects.</returns>
  31. /// <exception cref="ArgumentException"/>
  32. /// <exception cref="ArgumentNullException"/>
  33. /// <exception cref="DirectoryNotFoundException"/>
  34. /// <exception cref="IOException"/>
  35. /// <exception cref="NotSupportedException"/>
  36. /// <exception cref="UnauthorizedAccessException"/>
  37. /// <param name="path">The directory path.</param>
  38. /// <param name="options"><see cref="DirectoryEnumerationOptions"/> flags that specify how the directory is to be enumerated.</param>
  39. [SecurityCritical]
  40. public static long CountFileSystemObjects(string path, DirectoryEnumerationOptions options)
  41. {
  42. return EnumerateFileSystemEntryInfosCore<string>(null, path, Path.WildcardStarMatchAll, options, PathFormat.RelativePath).Count();
  43. }
  44. /// <summary>[AlphaFS] Counts file system objects: files, folders or both) in a given directory.</summary>
  45. /// <returns>The counted number of file system objects.</returns>
  46. /// <exception cref="ArgumentException"/>
  47. /// <exception cref="ArgumentNullException"/>
  48. /// <exception cref="DirectoryNotFoundException"/>
  49. /// <exception cref="IOException"/>
  50. /// <exception cref="NotSupportedException"/>
  51. /// <exception cref="UnauthorizedAccessException"/>
  52. /// <param name="path">The directory path.</param>
  53. /// <param name="options"><see cref="DirectoryEnumerationOptions"/> flags that specify how the directory is to be enumerated.</param>
  54. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  55. [SecurityCritical]
  56. public static long CountFileSystemObjects(string path, DirectoryEnumerationOptions options, PathFormat pathFormat)
  57. {
  58. return EnumerateFileSystemEntryInfosCore<string>(null, path, Path.WildcardStarMatchAll, options, pathFormat).Count();
  59. }
  60. /// <summary>[AlphaFS] Counts file system objects: files, folders or both) in a given directory.</summary>
  61. /// <returns>The counted number of file system objects.</returns>
  62. /// <exception cref="ArgumentException"/>
  63. /// <exception cref="ArgumentNullException"/>
  64. /// <exception cref="DirectoryNotFoundException"/>
  65. /// <exception cref="IOException"/>
  66. /// <exception cref="NotSupportedException"/>
  67. /// <exception cref="UnauthorizedAccessException"/>
  68. /// <param name="path">The directory path.</param>
  69. /// <param name="searchPattern">
  70. /// The search string to match against the names of directories in <paramref name="path"/>.
  71. /// This parameter can contain a combination of valid literal path and wildcard
  72. /// (<see cref="Path.WildcardStarMatchAll"/> and <see cref="Path.WildcardQuestion"/>) characters, but does not support regular expressions.
  73. /// </param>
  74. /// <param name="options"><see cref="DirectoryEnumerationOptions"/> flags that specify how the directory is to be enumerated.</param>
  75. [SecurityCritical]
  76. public static long CountFileSystemObjects(string path, string searchPattern, DirectoryEnumerationOptions options)
  77. {
  78. return EnumerateFileSystemEntryInfosCore<string>(null, path, searchPattern, options, PathFormat.RelativePath).Count();
  79. }
  80. /// <summary>[AlphaFS] Counts file system objects: files, folders or both) in a given directory.</summary>
  81. /// <returns>The counted number of file system objects.</returns>
  82. /// <exception cref="ArgumentException"/>
  83. /// <exception cref="ArgumentNullException"/>
  84. /// <exception cref="DirectoryNotFoundException"/>
  85. /// <exception cref="IOException"/>
  86. /// <exception cref="NotSupportedException"/>
  87. /// <exception cref="UnauthorizedAccessException"/>
  88. /// <param name="path">The directory path.</param>
  89. /// <param name="searchPattern">
  90. /// The search string to match against the names of directories in <paramref name="path"/>.
  91. /// This parameter can contain a combination of valid literal path and wildcard
  92. /// (<see cref="Path.WildcardStarMatchAll"/> and <see cref="Path.WildcardQuestion"/>) characters, but does not support regular expressions.
  93. /// </param>
  94. /// <param name="options"><see cref="DirectoryEnumerationOptions"/> flags that specify how the directory is to be enumerated.</param>
  95. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  96. [SecurityCritical]
  97. public static long CountFileSystemObjects(string path, string searchPattern, DirectoryEnumerationOptions options, PathFormat pathFormat)
  98. {
  99. return EnumerateFileSystemEntryInfosCore<string>(null, path, searchPattern, options, pathFormat).Count();
  100. }
  101. #region Transactional
  102. /// <summary>[AlphaFS] Counts file system objects: files, folders or both) in a given directory.</summary>
  103. /// <returns>The counted number of file system objects.</returns>
  104. /// <exception cref="ArgumentException"/>
  105. /// <exception cref="ArgumentNullException"/>
  106. /// <exception cref="DirectoryNotFoundException"/>
  107. /// <exception cref="IOException"/>
  108. /// <exception cref="NotSupportedException"/>
  109. /// <exception cref="UnauthorizedAccessException"/>
  110. /// <param name="transaction">The transaction.</param>
  111. /// <param name="path">The directory path.</param>
  112. /// <param name="options"><see cref="DirectoryEnumerationOptions"/> flags that specify how the directory is to be enumerated.</param>
  113. [SecurityCritical]
  114. public static long CountFileSystemObjectsTransacted(KernelTransaction transaction, string path, DirectoryEnumerationOptions options)
  115. {
  116. return EnumerateFileSystemEntryInfosCore<string>(transaction, path, Path.WildcardStarMatchAll, options, PathFormat.RelativePath).Count();
  117. }
  118. /// <summary>[AlphaFS] Counts file system objects: files, folders or both) in a given directory.</summary>
  119. /// <returns>The counted number of file system objects.</returns>
  120. /// <exception cref="ArgumentException"/>
  121. /// <exception cref="ArgumentNullException"/>
  122. /// <exception cref="DirectoryNotFoundException"/>
  123. /// <exception cref="IOException"/>
  124. /// <exception cref="NotSupportedException"/>
  125. /// <exception cref="UnauthorizedAccessException"/>
  126. /// <param name="transaction">The transaction.</param>
  127. /// <param name="path">The directory path.</param>
  128. /// <param name="options"><see cref="DirectoryEnumerationOptions"/> flags that specify how the directory is to be enumerated.</param>
  129. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  130. [SecurityCritical]
  131. public static long CountFileSystemObjectsTransacted(KernelTransaction transaction, string path, DirectoryEnumerationOptions options, PathFormat pathFormat)
  132. {
  133. return EnumerateFileSystemEntryInfosCore<string>(transaction, path, Path.WildcardStarMatchAll, options, pathFormat).Count();
  134. }
  135. /// <summary>[AlphaFS] Counts file system objects: files, folders or both) in a given directory.</summary>
  136. /// <returns>The counted number of file system objects.</returns>
  137. /// <exception cref="ArgumentException"/>
  138. /// <exception cref="ArgumentNullException"/>
  139. /// <exception cref="DirectoryNotFoundException"/>
  140. /// <exception cref="IOException"/>
  141. /// <exception cref="NotSupportedException"/>
  142. /// <exception cref="UnauthorizedAccessException"/>
  143. /// <param name="transaction">The transaction.</param>
  144. /// <param name="path">The directory path.</param>
  145. /// <param name="searchPattern">
  146. /// The search string to match against the names of directories in <paramref name="path"/>.
  147. /// This parameter can contain a combination of valid literal path and wildcard
  148. /// (<see cref="Path.WildcardStarMatchAll"/> and <see cref="Path.WildcardQuestion"/>) characters, but does not support regular expressions.
  149. /// </param>
  150. /// <param name="options"><see cref="DirectoryEnumerationOptions"/> flags that specify how the directory is to be enumerated.</param>
  151. [SecurityCritical]
  152. public static long CountFileSystemObjectsTransacted(KernelTransaction transaction, string path, string searchPattern, DirectoryEnumerationOptions options)
  153. {
  154. return EnumerateFileSystemEntryInfosCore<string>(transaction, path, searchPattern, options, PathFormat.RelativePath).Count();
  155. }
  156. /// <summary>[AlphaFS] Counts file system objects: files, folders or both) in a given directory.</summary>
  157. /// <returns>The counted number of file system objects.</returns>
  158. /// <exception cref="ArgumentException"/>
  159. /// <exception cref="ArgumentNullException"/>
  160. /// <exception cref="DirectoryNotFoundException"/>
  161. /// <exception cref="IOException"/>
  162. /// <exception cref="NotSupportedException"/>
  163. /// <exception cref="UnauthorizedAccessException"/>
  164. /// <param name="transaction">The transaction.</param>
  165. /// <param name="path">The directory path.</param>
  166. /// <param name="searchPattern">
  167. /// The search string to match against the names of directories in <paramref name="path"/>.
  168. /// This parameter can contain a combination of valid literal path and wildcard
  169. /// (<see cref="Path.WildcardStarMatchAll"/> and <see cref="Path.WildcardQuestion"/>) characters, but does not support regular expressions.
  170. /// </param>
  171. /// <param name="options"><see cref="DirectoryEnumerationOptions"/> flags that specify how the directory is to be enumerated.</param>
  172. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  173. [SecurityCritical]
  174. public static long CountFileSystemObjectsTransacted(KernelTransaction transaction, string path, string searchPattern, DirectoryEnumerationOptions options, PathFormat pathFormat)
  175. {
  176. return EnumerateFileSystemEntryInfosCore<string>(transaction, path, searchPattern, options, pathFormat).Count();
  177. }
  178. #endregion // Transactional
  179. }
  180. }