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

File.GetFileSystemEntryInfo.cs 5.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.Security;
  23. namespace Alphaleonis.Win32.Filesystem
  24. {
  25. public static partial class File
  26. {
  27. #region GetFileSystemEntry
  28. /// <summary>[AlphaFS] Gets the <see cref="FileSystemEntryInfo"/> of the file on the path.</summary>
  29. /// <returns>The <see cref="FileSystemEntryInfo"/> instance of the file or directory.</returns>
  30. /// <param name="path">The path to the file or directory.</param>
  31. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  32. [SecurityCritical]
  33. public static FileSystemEntryInfo GetFileSystemEntryInfo(string path, PathFormat pathFormat)
  34. {
  35. return GetFileSystemEntryInfoCore(false, null, path, false, pathFormat);
  36. }
  37. /// <summary>[AlphaFS] Gets the <see cref="FileSystemEntryInfo"/> of the file on the path.</summary>
  38. /// <returns>The <see cref="FileSystemEntryInfo"/> instance of the file or directory.</returns>
  39. /// <param name="path">The path to the file or directory.</param>
  40. [SecurityCritical]
  41. public static FileSystemEntryInfo GetFileSystemEntryInfo(string path)
  42. {
  43. return GetFileSystemEntryInfoCore(false, null, path, false, PathFormat.RelativePath);
  44. }
  45. /// <summary>[AlphaFS] Gets the <see cref="FileSystemEntryInfo"/> of the file on the path.</summary>
  46. /// <returns>The <see cref="FileSystemEntryInfo"/> instance of the file or directory.</returns>
  47. /// <param name="transaction">The transaction.</param>
  48. /// <param name="path">The path to the file or directory.</param>
  49. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  50. [SecurityCritical]
  51. public static FileSystemEntryInfo GetFileSystemEntryInfoTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  52. {
  53. return GetFileSystemEntryInfoCore(false, transaction, path, false, pathFormat);
  54. }
  55. /// <summary>[AlphaFS] Gets the <see cref="FileSystemEntryInfo"/> of the file on the path.</summary>
  56. /// <returns>The <see cref="FileSystemEntryInfo"/> instance of the file or directory.</returns>
  57. /// <param name="transaction">The transaction.</param>
  58. /// <param name="path">The path to the file or directory.</param>
  59. [SecurityCritical]
  60. public static FileSystemEntryInfo GetFileSystemEntryInfoTransacted(KernelTransaction transaction, string path)
  61. {
  62. return GetFileSystemEntryInfoCore(false, transaction, path, false, PathFormat.RelativePath);
  63. }
  64. #endregion // GetFileSystemEntry
  65. #region Internal Methods
  66. /// <summary>Gets a FileSystemEntryInfo from a Non-/Transacted directory/file.</summary>
  67. /// <returns>The <see cref="FileSystemEntryInfo"/> instance of the file or directory, or <c>null</c> on Exception when <paramref name="continueOnException"/> is <c>true</c>.</returns>
  68. /// <remarks>BasicSearch <see cref="NativeMethods.FINDEX_INFO_LEVELS.Basic"/> and LargeCache <see cref="NativeMethods.FindExAdditionalFlags.LargeFetch"/> are used by default, if possible.</remarks>
  69. /// <exception cref="ArgumentException"/>
  70. /// <exception cref="ArgumentNullException"/>
  71. /// <param name="isFolder">Specifies that <paramref name="path"/> is a file or directory.</param>
  72. /// <param name="transaction">The transaction.</param>
  73. /// <param name="path">The path to the file or directory.</param>
  74. /// <param name="continueOnException">
  75. /// <para><c>true</c> suppress any Exception that might be thrown as a result from a failure,</para>
  76. /// <para>such as ACLs protected directories or non-accessible reparse points.</para>
  77. /// </param>
  78. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  79. [SecurityCritical]
  80. internal static FileSystemEntryInfo GetFileSystemEntryInfoCore(bool isFolder, KernelTransaction transaction, string path, bool continueOnException, PathFormat pathFormat)
  81. {
  82. // Enable BasicSearch and LargeCache by default.
  83. var options = DirectoryEnumerationOptions.BasicSearch | DirectoryEnumerationOptions.LargeCache | (continueOnException ? DirectoryEnumerationOptions.ContinueOnException : 0);
  84. return (new FindFileSystemEntryInfo(isFolder, transaction, path, Path.WildcardStarMatchAll, options, typeof(FileSystemEntryInfo), pathFormat)).Get<FileSystemEntryInfo>();
  85. }
  86. #endregion // Internal Methods
  87. }
  88. }