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.

Directory.GetFiles.cs 14 KiB

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