Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

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