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.
 
 

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