Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DirectoryInfo.GetFileSystemInfos.cs 8.0 KiB

há 7 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.Diagnostics.CodeAnalysis;
  23. using System.IO;
  24. using System.Linq;
  25. using System.Security;
  26. namespace Alphaleonis.Win32.Filesystem
  27. {
  28. partial class DirectoryInfo
  29. {
  30. #region .NET
  31. /// <summary>Returns an array of strongly typed <see cref="FileSystemInfo"/> entries representing all the files and subdirectories in a directory.</summary>
  32. /// <returns>An array of strongly typed <see cref="FileSystemInfo"/> entries.</returns>
  33. /// <remarks>
  34. /// For subdirectories, the <see cref="FileSystemInfo"/> objects returned by this method can be cast to the derived class <see cref="DirectoryInfo"/>.
  35. /// Use the <see cref="FileAttributes"/> value returned by the <see cref="FileSystemInfo.Attributes"/> property to determine whether the <see cref="FileSystemInfo"/> represents a file or a directory.
  36. /// </remarks>
  37. /// <remarks>
  38. /// If there are no files or directories in the DirectoryInfo, this method returns an empty array. This method is not recursive.
  39. /// For subdirectories, the FileSystemInfo objects returned by this method can be cast to the derived class DirectoryInfo.
  40. /// Use the FileAttributes value returned by the Attributes property to determine whether the FileSystemInfo represents a file or a directory.
  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. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Infos")]
  49. [SecurityCritical]
  50. public FileSystemInfo[] GetFileSystemInfos()
  51. {
  52. return Directory.EnumerateFileSystemEntryInfosCore<FileSystemInfo>(Transaction, LongFullName, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.LongFullPath).ToArray();
  53. }
  54. /// <summary>Retrieves an array of strongly typed <see cref="FileSystemInfo"/> objects representing the files and subdirectories that match the specified search criteria.</summary>
  55. /// <param name="searchPattern">
  56. /// The search string to match against the names of directories in path.
  57. /// This parameter can contain a combination of valid literal path and wildcard
  58. /// (<see cref="Path.WildcardStarMatchAll"/> and <see cref="Path.WildcardQuestion"/>) characters, but does not support regular expressions.
  59. /// </param>
  60. /// <returns>An array of strongly typed <see cref="FileSystemInfo"/> entries.</returns>
  61. /// <remarks>
  62. /// For subdirectories, the <see cref="FileSystemInfo"/> objects returned by this method can be cast to the derived class <see cref="DirectoryInfo"/>.
  63. /// Use the <see cref="FileAttributes"/> value returned by the <see cref="FileSystemInfo.Attributes"/> property to determine whether the <see cref="FileSystemInfo"/> represents a file or a directory.
  64. /// </remarks>
  65. /// <remarks>
  66. /// If there are no files or directories in the DirectoryInfo, this method returns an empty array. This method is not recursive.
  67. /// For subdirectories, the FileSystemInfo objects returned by this method can be cast to the derived class DirectoryInfo.
  68. /// Use the FileAttributes value returned by the Attributes property to determine whether the FileSystemInfo represents a file or a directory.
  69. /// </remarks>
  70. /// <exception cref="ArgumentException"/>
  71. /// <exception cref="ArgumentNullException"/>
  72. /// <exception cref="DirectoryNotFoundException"/>
  73. /// <exception cref="IOException"/>
  74. /// <exception cref="NotSupportedException"/>
  75. /// <exception cref="UnauthorizedAccessException"/>
  76. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Infos")]
  77. [SecurityCritical]
  78. public FileSystemInfo[] GetFileSystemInfos(string searchPattern)
  79. {
  80. return Directory.EnumerateFileSystemEntryInfosCore<FileSystemInfo>(Transaction, LongFullName, searchPattern, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.LongFullPath).ToArray();
  81. }
  82. /// <summary>Retrieves an array of strongly typed <see cref="FileSystemInfo"/> objects representing the files and subdirectories that match the specified search criteria.</summary>
  83. /// <param name="searchPattern">
  84. /// The search string to match against the names of directories in path.
  85. /// This parameter can contain a combination of valid literal path and wildcard
  86. /// (<see cref="Path.WildcardStarMatchAll"/> and <see cref="Path.WildcardQuestion"/>) characters, but does not support regular expressions.
  87. /// </param>
  88. /// <param name="searchOption">
  89. /// One of the <see cref="SearchOption"/> enumeration values that specifies whether the <paramref name="searchOption"/>
  90. /// should include only the current directory or should include all subdirectories.
  91. /// </param>
  92. /// <returns>An array of strongly typed <see cref="FileSystemInfo"/> entries.</returns>
  93. /// <remarks>
  94. /// For subdirectories, the <see cref="FileSystemInfo"/> objects returned by this method can be cast to the derived class <see cref="DirectoryInfo"/>.
  95. /// Use the <see cref="FileAttributes"/> value returned by the <see cref="FileSystemInfo.Attributes"/> property to determine whether the <see cref="FileSystemInfo"/> represents a file or a directory.
  96. /// </remarks>
  97. /// <remarks>
  98. /// If there are no files or directories in the DirectoryInfo, this method returns an empty array. This method is not recursive.
  99. /// For subdirectories, the FileSystemInfo objects returned by this method can be cast to the derived class DirectoryInfo.
  100. /// Use the FileAttributes value returned by the Attributes property to determine whether the FileSystemInfo represents a file or a directory.
  101. /// </remarks>
  102. /// <exception cref="ArgumentException"/>
  103. /// <exception cref="ArgumentNullException"/>
  104. /// <exception cref="DirectoryNotFoundException"/>
  105. /// <exception cref="IOException"/>
  106. /// <exception cref="NotSupportedException"/>
  107. /// <exception cref="UnauthorizedAccessException"/>
  108. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Infos")]
  109. [SecurityCritical]
  110. public FileSystemInfo[] GetFileSystemInfos(string searchPattern, SearchOption searchOption)
  111. {
  112. var options = DirectoryEnumerationOptions.FilesAndFolders | ((searchOption == SearchOption.AllDirectories) ? DirectoryEnumerationOptions.Recursive : 0);
  113. return Directory.EnumerateFileSystemEntryInfosCore<FileSystemInfo>(Transaction, LongFullName, searchPattern, options, PathFormat.LongFullPath).ToArray();
  114. }
  115. #endregion // .NET
  116. }
  117. }