25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

211 lines
14 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. using SearchOption = System.IO.SearchOption;
  26. namespace Alphaleonis.Win32.Filesystem
  27. {
  28. partial class Directory
  29. {
  30. // Since Directory.GetDirectories() is less efficient than Directory.EnumerateDirectories(),
  31. // only .NET and AlphaFS Transactional methods are implemented. No additional overloaded methods.
  32. #region .NET
  33. /// <summary>Returns the names of subdirectories (including their paths) in the specified directory.</summary>
  34. /// <returns>An array of the full names (including paths) of subdirectories in the specified path, or an empty array if no directories are found.</returns>
  35. /// <remarks>
  36. /// <para>The names returned by this method are prefixed with the directory information provided in path.</para>
  37. /// <para>The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names
  38. /// before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array.
  39. /// Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.
  40. /// </para>
  41. /// </remarks>
  42. /// <exception cref="ArgumentException"/>
  43. /// <exception cref="ArgumentNullException"/>
  44. /// <exception cref="DirectoryNotFoundException"/>
  45. /// <exception cref="IOException"/>
  46. /// <exception cref="NotSupportedException"/>
  47. /// <exception cref="UnauthorizedAccessException"/>
  48. /// <param name="path">The directory to search.</param>
  49. [SecurityCritical]
  50. public static string[] GetDirectories(string path)
  51. {
  52. return EnumerateFileSystemEntryInfosCore<string>(null, path, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.Folders, PathFormat.RelativePath).ToArray();
  53. }
  54. /// <summary>Returns the names of subdirectories (including their paths) that match the specified search pattern in the specified directory.</summary>
  55. /// <returns>An array of the full names (including paths) of the subdirectories that match the search pattern in the specified directory, or an empty array if no directories are found.</returns>
  56. /// <remarks>
  57. /// <para>The names returned by this method are prefixed with the directory information provided in path.</para>
  58. /// <para>The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names
  59. /// before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array.
  60. /// Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.
  61. /// </para>
  62. /// </remarks>
  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="path">The directory to search.</param>
  70. /// <param name="searchPattern">
  71. /// The search string to match against the names of directories in <paramref name="path"/>.
  72. /// This parameter can contain a combination of valid literal path and wildcard
  73. /// (<see cref="Path.WildcardStarMatchAll"/> and <see cref="Path.WildcardQuestion"/>) characters, but does not support regular expressions.
  74. /// </param>
  75. [SecurityCritical]
  76. public static string[] GetDirectories(string path, string searchPattern)
  77. {
  78. return EnumerateFileSystemEntryInfosCore<string>(null, path, searchPattern, DirectoryEnumerationOptions.Folders, PathFormat.RelativePath).ToArray();
  79. }
  80. /// <summary>Returns the names of the subdirectories (including their paths) that match the specified search pattern in the specified directory, and optionally searches subdirectories.</summary>
  81. /// <returns>An array of the full names (including paths) of the subdirectories that match the specified criteria, or an empty array if no directories are found.</returns>
  82. /// <remarks>
  83. /// <para>The names returned by this method are prefixed with the directory information provided in path.</para>
  84. /// <para>The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names
  85. /// before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array.
  86. /// Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.
  87. /// </para>
  88. /// </remarks>
  89. /// <exception cref="ArgumentException"/>
  90. /// <exception cref="ArgumentNullException"/>
  91. /// <exception cref="DirectoryNotFoundException"/>
  92. /// <exception cref="IOException"/>
  93. /// <exception cref="NotSupportedException"/>
  94. /// <exception cref="UnauthorizedAccessException"/>
  95. /// <param name="path">The directory to search.</param>
  96. /// <param name="searchPattern">
  97. /// The search string to match against the names of directories in <paramref name="path"/>.
  98. /// This parameter can contain a combination of valid literal path and wildcard
  99. /// (<see cref="Path.WildcardStarMatchAll"/> and <see cref="Path.WildcardQuestion"/>) characters, but does not support regular expressions.
  100. /// </param>
  101. /// <param name="searchOption">
  102. /// One of the <see cref="SearchOption"/> enumeration values that specifies whether the <paramref name="searchOption"/>
  103. /// should include only the current directory or should include all subdirectories.
  104. /// </param>
  105. [SecurityCritical]
  106. public static string[] GetDirectories(string path, string searchPattern, SearchOption searchOption)
  107. {
  108. var options = DirectoryEnumerationOptions.Folders | ((searchOption == SearchOption.AllDirectories) ? DirectoryEnumerationOptions.Recursive : 0);
  109. return EnumerateFileSystemEntryInfosCore<string>(null, path, searchPattern, options, PathFormat.RelativePath).ToArray();
  110. }
  111. #endregion // .NET
  112. #region Transactional
  113. /// <summary>Returns the names of subdirectories (including their paths) in the specified directory.</summary>
  114. /// <returns>An array of the full names (including paths) of subdirectories in the specified path, or an empty array if no directories are found.</returns>
  115. /// <remarks>
  116. /// <para>The names returned by this method are prefixed with the directory information provided in path.</para>
  117. /// <para>The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names
  118. /// before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array.
  119. /// Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.
  120. /// </para>
  121. /// </remarks>
  122. /// <exception cref="ArgumentException"/>
  123. /// <exception cref="ArgumentNullException"/>
  124. /// <exception cref="DirectoryNotFoundException"/>
  125. /// <exception cref="IOException"/>
  126. /// <exception cref="NotSupportedException"/>
  127. /// <exception cref="UnauthorizedAccessException"/>
  128. /// <param name="transaction">The transaction.</param>
  129. /// <param name="path">The directory to search.</param>
  130. [SecurityCritical]
  131. public static string[] GetDirectoriesTransacted(KernelTransaction transaction, string path)
  132. {
  133. return EnumerateFileSystemEntryInfosCore<string>(transaction, path, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.Folders, PathFormat.RelativePath).ToArray();
  134. }
  135. /// <summary>Returns the names of subdirectories (including their paths) that match the specified search pattern in the specified directory.</summary>
  136. /// <returns>An array of the full names (including paths) of the subdirectories that match the search pattern in the specified directory, or an empty array if no directories are found.</returns>
  137. /// <remarks>
  138. /// <para>The names returned by this method are prefixed with the directory information provided in path.</para>
  139. /// <para>The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names
  140. /// before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array.
  141. /// Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.
  142. /// </para>
  143. /// </remarks>
  144. /// <exception cref="ArgumentException"/>
  145. /// <exception cref="ArgumentNullException"/>
  146. /// <exception cref="DirectoryNotFoundException"/>
  147. /// <exception cref="IOException"/>
  148. /// <exception cref="NotSupportedException"/>
  149. /// <exception cref="UnauthorizedAccessException"/>
  150. /// <param name="transaction">The transaction.</param>
  151. /// <param name="path">The directory to search.</param>
  152. /// <param name="searchPattern">
  153. /// The search string to match against the names of directories in <paramref name="path"/>.
  154. /// This parameter can contain a combination of valid literal path and wildcard
  155. /// (<see cref="Path.WildcardStarMatchAll"/> and <see cref="Path.WildcardQuestion"/>) characters, but does not support regular expressions.
  156. /// </param>
  157. [SecurityCritical]
  158. public static string[] GetDirectoriesTransacted(KernelTransaction transaction, string path, string searchPattern)
  159. {
  160. return EnumerateFileSystemEntryInfosCore<string>(transaction, path, searchPattern, DirectoryEnumerationOptions.Folders, PathFormat.RelativePath).ToArray();
  161. }
  162. /// <summary>Returns the names of the subdirectories (including their paths) that match the specified search pattern in the specified directory, and optionally searches subdirectories.</summary>
  163. /// <returns>An array of the full names (including paths) of the subdirectories that match the specified criteria, or an empty array if no directories are found.</returns>
  164. /// <remarks>
  165. /// <para>The names returned by this method are prefixed with the directory information provided in path.</para>
  166. /// <para>The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names
  167. /// before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array.
  168. /// Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.
  169. /// </para>
  170. /// </remarks>
  171. /// <exception cref="ArgumentException"/>
  172. /// <exception cref="ArgumentNullException"/>
  173. /// <exception cref="DirectoryNotFoundException"/>
  174. /// <exception cref="IOException"/>
  175. /// <exception cref="NotSupportedException"/>
  176. /// <exception cref="UnauthorizedAccessException"/>
  177. /// <param name="transaction">The transaction.</param>
  178. /// <param name="path">The directory to search.</param>
  179. /// <param name="searchPattern">
  180. /// The search string to match against the names of directories in <paramref name="path"/>.
  181. /// This parameter can contain a combination of valid literal path and wildcard
  182. /// (<see cref="Path.WildcardStarMatchAll"/> and <see cref="Path.WildcardQuestion"/>) characters, but does not support regular expressions.
  183. /// </param>
  184. /// <param name="searchOption">
  185. /// One of the <see cref="SearchOption"/> enumeration values that specifies whether the <paramref name="searchOption"/>
  186. /// should include only the current directory or should include all subdirectories.
  187. /// </param>
  188. [SecurityCritical]
  189. public static string[] GetDirectoriesTransacted(KernelTransaction transaction, string path, string searchPattern, SearchOption searchOption)
  190. {
  191. var options = DirectoryEnumerationOptions.Folders | ((searchOption == SearchOption.AllDirectories) ? DirectoryEnumerationOptions.Recursive : 0);
  192. return EnumerateFileSystemEntryInfosCore<string>(transaction, path, searchPattern, options, PathFormat.RelativePath).ToArray();
  193. }
  194. #endregion // Transactional
  195. }
  196. }