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.

DirectoryInfo.EnumerateDirectories.cs 7.7 KiB

7 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.Collections.Generic;
  23. using System.IO;
  24. using System.Security;
  25. namespace Alphaleonis.Win32.Filesystem
  26. {
  27. partial class DirectoryInfo
  28. {
  29. #region .NET
  30. /// <summary>Returns an enumerable collection of directory information in the current directory.</summary>
  31. /// <returns>An enumerable collection of directories in the current directory.</returns>
  32. /// <exception cref="ArgumentException"/>
  33. /// <exception cref="ArgumentNullException"/>
  34. /// <exception cref="DirectoryNotFoundException"/>
  35. /// <exception cref="IOException"/>
  36. /// <exception cref="NotSupportedException"/>
  37. /// <exception cref="UnauthorizedAccessException"/>
  38. [SecurityCritical]
  39. public IEnumerable<DirectoryInfo> EnumerateDirectories()
  40. {
  41. return Directory.EnumerateFileSystemEntryInfosCore<DirectoryInfo>(Transaction, LongFullName, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.Folders, PathFormat.LongFullPath);
  42. }
  43. /// <summary>Returns an enumerable collection of directory information that matches a specified search pattern.</summary>
  44. /// <returns>An enumerable collection of directories that matches <paramref name="searchPattern"/>.</returns>
  45. /// <exception cref="ArgumentException"/>
  46. /// <exception cref="ArgumentNullException"/>
  47. /// <exception cref="DirectoryNotFoundException"/>
  48. /// <exception cref="IOException"/>
  49. /// <exception cref="NotSupportedException"/>
  50. /// <exception cref="UnauthorizedAccessException"/>
  51. /// <param name="searchPattern">
  52. /// The search string to match against the names of directories in path.
  53. /// This parameter can contain a combination of valid literal path and wildcard
  54. /// (<see cref="Path.WildcardStarMatchAll"/> and <see cref="Path.WildcardQuestion"/>) characters, but does not support regular expressions.
  55. /// </param>
  56. [SecurityCritical]
  57. public IEnumerable<DirectoryInfo> EnumerateDirectories(string searchPattern)
  58. {
  59. return Directory.EnumerateFileSystemEntryInfosCore<DirectoryInfo>(Transaction, LongFullName, searchPattern, DirectoryEnumerationOptions.Folders, PathFormat.LongFullPath);
  60. }
  61. /// <summary>Returns an enumerable collection of directory information that matches a specified search pattern and search subdirectory option.</summary>
  62. /// <returns>An enumerable collection of directories that matches <paramref name="searchPattern"/> and <paramref name="searchOption"/>.</returns>
  63. /// <exception cref="ArgumentException"/>
  64. /// <exception cref="ArgumentNullException"/>
  65. /// <exception cref="DirectoryNotFoundException"/>
  66. /// <exception cref="IOException"/>
  67. /// <exception cref="NotSupportedException"/>
  68. /// <exception cref="UnauthorizedAccessException"/>
  69. /// <param name="searchPattern">
  70. /// The search string to match against the names of directories in 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="searchOption">
  75. /// One of the <see cref="SearchOption"/> enumeration values that specifies whether the <paramref name="searchOption"/>
  76. /// should include only the current directory or should include all subdirectories.
  77. /// </param>
  78. [SecurityCritical]
  79. public IEnumerable<DirectoryInfo> EnumerateDirectories(string searchPattern, SearchOption searchOption)
  80. {
  81. var options = DirectoryEnumerationOptions.Folders | ((searchOption == SearchOption.AllDirectories) ? DirectoryEnumerationOptions.Recursive : 0);
  82. return Directory.EnumerateFileSystemEntryInfosCore<DirectoryInfo>(Transaction, LongFullName, searchPattern, options, PathFormat.LongFullPath);
  83. }
  84. #endregion // .NET
  85. /// <summary>[AlphaFS] Returns an enumerable collection of directory information in the current directory.</summary>
  86. /// <returns>An enumerable collection of directories in the current directory.</returns>
  87. /// <exception cref="ArgumentException"/>
  88. /// <exception cref="ArgumentNullException"/>
  89. /// <exception cref="DirectoryNotFoundException"/>
  90. /// <exception cref="IOException"/>
  91. /// <exception cref="NotSupportedException"/>
  92. /// <exception cref="UnauthorizedAccessException"/>
  93. /// <param name="options"><see cref="DirectoryEnumerationOptions"/> flags that specify how the directory is to be enumerated.</param>
  94. [SecurityCritical]
  95. public IEnumerable<DirectoryInfo> EnumerateDirectories(DirectoryEnumerationOptions options)
  96. {
  97. // Adhere to the method name.
  98. options &= ~DirectoryEnumerationOptions.Files;
  99. options |= DirectoryEnumerationOptions.Folders;
  100. return Directory.EnumerateFileSystemEntryInfosCore<DirectoryInfo>(Transaction, LongFullName, Path.WildcardStarMatchAll, options, PathFormat.LongFullPath);
  101. }
  102. /// <summary>Returns an enumerable collection of directory information that matches a specified search pattern.</summary>
  103. /// <returns>An enumerable collection of directories that matches <paramref name="searchPattern"/>.</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="searchPattern">
  111. /// The search string to match against the names of directories in path.
  112. /// This parameter can contain a combination of valid literal path and wildcard
  113. /// (<see cref="Path.WildcardStarMatchAll"/> and <see cref="Path.WildcardQuestion"/>) characters, but does not support regular expressions.
  114. /// </param>
  115. /// <param name="options"><see cref="DirectoryEnumerationOptions"/> flags that specify how the directory is to be enumerated.</param>
  116. [SecurityCritical]
  117. public IEnumerable<DirectoryInfo> EnumerateDirectories(string searchPattern, DirectoryEnumerationOptions options)
  118. {
  119. // Adhere to the method name.
  120. options &= ~DirectoryEnumerationOptions.Files;
  121. options |= DirectoryEnumerationOptions.Folders;
  122. return Directory.EnumerateFileSystemEntryInfosCore<DirectoryInfo>(Transaction, LongFullName, searchPattern, DirectoryEnumerationOptions.Folders | options, PathFormat.LongFullPath);
  123. }
  124. }
  125. }