Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

211 řádky
13 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.GetFileSystemEntries() is less efficient than Directory.EnumerateFileSystemEntries(),
  31. // only .NET and AlphaFS Transactional methods are implemented. No additional overloaded methods.
  32. #region .NET
  33. /// <summary>Returns the names of all files and subdirectories in the specified directory.</summary>
  34. /// <returns>An string[] array of the names of files and subdirectories in the specified directory.</returns>
  35. /// <remarks>
  36. /// <para>The EnumerateFileSystemEntries and GetFileSystemEntries methods differ as follows: When you use EnumerateFileSystemEntries,
  37. /// you can start enumerating the collection of entries before the whole collection is returned; when you use GetFileSystemEntries,
  38. /// you must wait for the whole array of entries to be returned before you can access the array.
  39. /// Therefore, when you are working with many files and directories, EnumerateFiles 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 for which file and subdirectory names are returned.</param>
  49. [SecurityCritical]
  50. public static string[] GetFileSystemEntries(string path)
  51. {
  52. return EnumerateFileSystemEntryInfosCore<string>(null, path, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.RelativePath).ToArray();
  53. }
  54. /// <summary>Returns an array of file system entries that match the specified search criteria.</summary>
  55. /// <returns>An string[] array of file system entries that match the specified search criteria.</returns>
  56. /// <remarks>
  57. /// <para>The EnumerateFileSystemEntries and GetFileSystemEntries methods differ as follows: When you use EnumerateFileSystemEntries,
  58. /// you can start enumerating the collection of entries before the whole collection is returned; when you use GetFileSystemEntries,
  59. /// you must wait for the whole array of entries to be returned before you can access the array.
  60. /// Therefore, when you are working with many files and directories, EnumerateFiles 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 path to be searched.</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[] GetFileSystemEntries(string path, string searchPattern)
  77. {
  78. return EnumerateFileSystemEntryInfosCore<string>(null, path, searchPattern, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.RelativePath).ToArray();
  79. }
  80. /// <summary>Gets an array of all the file names and directory names that match a <paramref name="searchPattern"/> in a specified path, and optionally searches subdirectories.</summary>
  81. /// <returns>An string[] array of file system entries that match the specified search criteria.</returns>
  82. /// <remarks>
  83. /// <para>The EnumerateFileSystemEntries and GetFileSystemEntries methods differ as follows: When you use EnumerateFileSystemEntries,
  84. /// you can start enumerating the collection of entries before the whole collection is returned; when you use GetFileSystemEntries,
  85. /// you must wait for the whole array of entries to be returned before you can access the array.
  86. /// Therefore, when you are working with many files and directories, EnumerateFiles 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[] GetFileSystemEntries(string path, string searchPattern, SearchOption searchOption)
  107. {
  108. var options = DirectoryEnumerationOptions.FilesAndFolders | ((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 all files and subdirectories in the specified directory.</summary>
  114. /// <returns>An string[] array of the names of files and subdirectories in the specified directory.</returns>
  115. /// <remarks>
  116. /// <para>The EnumerateFileSystemEntries and GetFileSystemEntries methods differ as follows: When you use EnumerateFileSystemEntries,
  117. /// you can start enumerating the collection of entries before the whole collection is returned; when you use GetFileSystemEntries,
  118. /// you must wait for the whole array of entries to be returned before you can access the array.
  119. /// Therefore, when you are working with many files and directories, EnumerateFiles 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 for which file and subdirectory names are returned.</param>
  130. [SecurityCritical]
  131. public static string[] GetFileSystemEntriesTransacted(KernelTransaction transaction, string path)
  132. {
  133. return EnumerateFileSystemEntryInfosCore<string>(transaction, path, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.RelativePath).ToArray();
  134. }
  135. /// <summary>Returns an array of file system entries that match the specified search criteria.</summary>
  136. /// <returns>An string[] array of file system entries that match the specified search criteria.</returns>
  137. /// <remarks>
  138. /// <para>The EnumerateFileSystemEntries and GetFileSystemEntries methods differ as follows: When you use EnumerateFileSystemEntries,
  139. /// you can start enumerating the collection of entries before the whole collection is returned; when you use GetFileSystemEntries,
  140. /// you must wait for the whole array of entries to be returned before you can access the array.
  141. /// Therefore, when you are working with many files and directories, EnumerateFiles 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 path to be searched.</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[] GetFileSystemEntriesTransacted(KernelTransaction transaction, string path, string searchPattern)
  159. {
  160. return EnumerateFileSystemEntryInfosCore<string>(transaction, path, searchPattern, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.RelativePath).ToArray();
  161. }
  162. /// <summary>Gets an array of all the file names and directory names that match a <paramref name="searchPattern"/> in a specified path, and optionally searches subdirectories.</summary>
  163. /// <returns>An string[] array of file system entries that match the specified search criteria.</returns>
  164. /// <remarks>
  165. /// <para>The EnumerateFileSystemEntries and GetFileSystemEntries methods differ as follows: When you use EnumerateFileSystemEntries,
  166. /// you can start enumerating the collection of entries before the whole collection is returned; when you use GetFileSystemEntries,
  167. /// you must wait for the whole array of entries to be returned before you can access the array.
  168. /// Therefore, when you are working with many files and directories, EnumerateFiles 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[] GetFileSystemEntriesTransacted(KernelTransaction transaction, string path, string searchPattern, SearchOption searchOption)
  190. {
  191. var options = DirectoryEnumerationOptions.FilesAndFolders | ((searchOption == SearchOption.AllDirectories) ? DirectoryEnumerationOptions.Recursive : 0);
  192. return EnumerateFileSystemEntryInfosCore<string>(transaction, path, searchPattern, options, PathFormat.RelativePath).ToArray();
  193. }
  194. #endregion // Transactional
  195. }
  196. }