Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

149 рядки
7.3 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.Collections.Generic;
  22. using System.Diagnostics.CodeAnalysis;
  23. using System.IO;
  24. using System.Security;
  25. using System.Text;
  26. using StreamReader = System.IO.StreamReader;
  27. namespace Alphaleonis.Win32.Filesystem
  28. {
  29. public static partial class File
  30. {
  31. #region ReadLines
  32. /// <summary>Reads the lines of a file.</summary>
  33. /// <param name="path">The file to read.</param>
  34. /// <returns>All the lines of the file, or the lines that are the result of a query.</returns>
  35. [SecurityCritical]
  36. public static IEnumerable<string> ReadLines(string path)
  37. {
  38. return ReadLinesCore(null, path, NativeMethods.DefaultFileEncoding, PathFormat.RelativePath);
  39. }
  40. /// <summary>Read the lines of a file that has a specified encoding.</summary>
  41. /// <param name="path">The file to read.</param>
  42. /// <param name="encoding">The encoding that is applied to the contents of the file.</param>
  43. /// <returns>All the lines of the file, or the lines that are the result of a query.</returns>
  44. [SecurityCritical]
  45. public static IEnumerable<string> ReadLines(string path, Encoding encoding)
  46. {
  47. return ReadLinesCore(null, path, encoding, PathFormat.RelativePath);
  48. }
  49. /// <summary>[AlphaFS] Reads the lines of a file.</summary>
  50. /// <param name="path">The file to read.</param>
  51. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  52. /// <returns>All the lines of the file, or the lines that are the result of a query.</returns>
  53. [SecurityCritical]
  54. public static IEnumerable<string> ReadLines(string path, PathFormat pathFormat)
  55. {
  56. return ReadLinesCore(null, path, NativeMethods.DefaultFileEncoding, pathFormat);
  57. }
  58. /// <summary>[AlphaFS] Read the lines of a file that has a specified encoding.</summary>
  59. /// <param name="path">The file to read.</param>
  60. /// <param name="encoding">The encoding that is applied to the contents of the file.</param>
  61. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  62. /// <returns>All the lines of the file, or the lines that are the result of a query.</returns>
  63. [SecurityCritical]
  64. public static IEnumerable<string> ReadLines(string path, Encoding encoding, PathFormat pathFormat)
  65. {
  66. return ReadLinesCore(null, path, encoding, pathFormat);
  67. }
  68. #region Transactional
  69. /// <summary>[AlphaFS] Reads the lines of a file.</summary>
  70. /// <param name="transaction">The transaction.</param>
  71. /// <param name="path">The file to read.</param>
  72. /// <returns>All the lines of the file, or the lines that are the result of a query.</returns>
  73. [SecurityCritical]
  74. public static IEnumerable<string> ReadLinesTransacted(KernelTransaction transaction, string path)
  75. {
  76. return ReadLinesCore(transaction, path, NativeMethods.DefaultFileEncoding, PathFormat.RelativePath);
  77. }
  78. /// <summary>[AlphaFS] Read the lines of a file that has a specified encoding.</summary>
  79. /// <param name="transaction">The transaction.</param>
  80. /// <param name="path">The file to read.</param>
  81. /// <param name="encoding">The encoding that is applied to the contents of the file.</param>
  82. /// <returns>All the lines of the file, or the lines that are the result of a query.</returns>
  83. [SecurityCritical]
  84. public static IEnumerable<string> ReadLinesTransacted(KernelTransaction transaction, string path, Encoding encoding)
  85. {
  86. return ReadLinesCore(transaction, path, encoding, PathFormat.RelativePath);
  87. }
  88. /// <summary>[AlphaFS] Reads the lines of a file.</summary>
  89. /// <param name="transaction">The transaction.</param>
  90. /// <param name="path">The file to read.</param>
  91. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  92. /// <returns>All the lines of the file, or the lines that are the result of a query.</returns>
  93. [SecurityCritical]
  94. public static IEnumerable<string> ReadLinesTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  95. {
  96. return ReadLinesCore(transaction, path, NativeMethods.DefaultFileEncoding, pathFormat);
  97. }
  98. /// <summary>[AlphaFS] Read the lines of a file that has a specified encoding.</summary>
  99. /// <param name="transaction">The transaction.</param>
  100. /// <param name="path">The file to read.</param>
  101. /// <param name="encoding">The encoding that is applied to the contents of the file.</param>
  102. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  103. /// <returns>All the lines of the file, or the lines that are the result of a query.</returns>
  104. [SecurityCritical]
  105. public static IEnumerable<string> ReadLinesTransacted(KernelTransaction transaction, string path, Encoding encoding, PathFormat pathFormat)
  106. {
  107. return ReadLinesCore(transaction, path, encoding, pathFormat);
  108. }
  109. #endregion // Transacted
  110. #endregion // ReadLines
  111. #region Internal Methods
  112. /// <summary>Reads the lines of a file that has a specified encoding.</summary>
  113. /// <param name="transaction">The transaction.</param>
  114. /// <param name="path">The file to read.</param>
  115. /// <param name="encoding">The encoding that is applied to the contents of the file.</param>
  116. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  117. /// <returns>All the lines of the file, or the lines that are the result of a query.</returns>
  118. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  119. [SecurityCritical]
  120. internal static IEnumerable<string> ReadLinesCore(KernelTransaction transaction, string path, Encoding encoding, PathFormat pathFormat)
  121. {
  122. using (StreamReader sr = new StreamReader(OpenCore(transaction, path, FileMode.Open, FileAccess.Read, FileShare.Read, ExtendedFileAttributes.SequentialScan, null, null, pathFormat), encoding))
  123. {
  124. string line;
  125. while ((line = sr.ReadLine()) != null)
  126. yield return line;
  127. }
  128. }
  129. #endregion // Internal Methods
  130. }
  131. }