Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

DirectoryInfo.GetFiles.cs 6.9 KiB

7 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 DirectoryInfo
  28. {
  29. #region .NET
  30. /// <summary>Returns a file list from the current directory.</summary>
  31. /// <returns>An array of type <see cref="FileInfo"/>.</returns>
  32. /// <remarks>The order of the returned file names is not guaranteed; use the Sort() method if a specific sort order is required.</remarks>
  33. /// <remarks>If there are no files in the <see cref="DirectoryInfo"/>, this method returns an empty array.</remarks>
  34. /// <remarks>
  35. /// The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names
  36. /// 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.
  37. /// Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.
  38. /// </remarks>
  39. /// <exception cref="ArgumentException"/>
  40. /// <exception cref="ArgumentNullException"/>
  41. /// <exception cref="DirectoryNotFoundException"/>
  42. /// <exception cref="IOException"/>
  43. /// <exception cref="NotSupportedException"/>
  44. /// <exception cref="UnauthorizedAccessException"/>
  45. [SecurityCritical]
  46. public FileInfo[] GetFiles()
  47. {
  48. return Directory.EnumerateFileSystemEntryInfosCore<FileInfo>(Transaction, LongFullName, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.Files, PathFormat.LongFullPath).ToArray();
  49. }
  50. /// <summary>Returns a file list from the current directory matching the given search pattern.</summary>
  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. /// <returns>An array of type <see cref="FileInfo"/>.</returns>
  57. /// <remarks>The order of the returned file names is not guaranteed; use the Sort() method if a specific sort order is required.</remarks>
  58. /// <remarks>If there are no files in the <see cref="DirectoryInfo"/>, this method returns an empty array.</remarks>
  59. /// <remarks>
  60. /// 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. /// </remarks>
  64. /// <exception cref="ArgumentException"/>
  65. /// <exception cref="ArgumentNullException"/>
  66. /// <exception cref="DirectoryNotFoundException"/>
  67. /// <exception cref="IOException"/>
  68. /// <exception cref="NotSupportedException"/>
  69. /// <exception cref="UnauthorizedAccessException"/>
  70. [SecurityCritical]
  71. public FileInfo[] GetFiles(string searchPattern)
  72. {
  73. return Directory.EnumerateFileSystemEntryInfosCore<FileInfo>(Transaction, LongFullName, searchPattern, DirectoryEnumerationOptions.Files, PathFormat.LongFullPath).ToArray();
  74. }
  75. /// <summary>Returns a file list from the current directory matching the given search pattern and using a value to determine whether to search subdirectories.</summary>
  76. /// <param name="searchPattern">
  77. /// The search string to match against the names of directories in path.
  78. /// This parameter can contain a combination of valid literal path and wildcard
  79. /// (<see cref="Path.WildcardStarMatchAll"/> and <see cref="Path.WildcardQuestion"/>) characters, but does not support regular expressions.
  80. /// </param>
  81. /// <param name="searchOption">
  82. /// One of the <see cref="SearchOption"/> enumeration values that specifies whether the <paramref name="searchOption"/>
  83. /// should include only the current directory or should include all subdirectories.
  84. /// </param>
  85. /// <returns>An array of type <see cref="FileInfo"/>.</returns>
  86. /// <remarks>The order of the returned file names is not guaranteed; use the Sort() method if a specific sort order is required.</remarks>
  87. /// <remarks>If there are no files in the <see cref="DirectoryInfo"/>, this method returns an empty array.</remarks>
  88. /// <remarks>
  89. /// The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names
  90. /// 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.
  91. /// Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.
  92. /// </remarks>
  93. /// <exception cref="ArgumentException"/>
  94. /// <exception cref="ArgumentNullException"/>
  95. /// <exception cref="DirectoryNotFoundException"/>
  96. /// <exception cref="IOException"/>
  97. /// <exception cref="NotSupportedException"/>
  98. /// <exception cref="UnauthorizedAccessException"/>
  99. [SecurityCritical]
  100. public FileInfo[] GetFiles(string searchPattern, SearchOption searchOption)
  101. {
  102. var options = DirectoryEnumerationOptions.Files | ((searchOption == SearchOption.AllDirectories) ? DirectoryEnumerationOptions.Recursive : 0);
  103. return Directory.EnumerateFileSystemEntryInfosCore<FileInfo>(Transaction, LongFullName, searchPattern, options, PathFormat.LongFullPath).ToArray();
  104. }
  105. #endregion // .NET
  106. }
  107. }