You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

File.OpenText.cs 7.7 KiB

пре 8 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. namespace Alphaleonis.Win32.Filesystem
  26. {
  27. public static partial class File
  28. {
  29. /// <summary>Opens an existing UTF-8 encoded text file for reading.</summary>
  30. /// <param name="path">The file to be opened for reading.</param>
  31. /// <returns>A <see cref="StreamReader"/> on the specified path.</returns>
  32. /// <remarks>This method is equivalent to the <see cref="StreamReader"/>(String) constructor overload.</remarks>
  33. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  34. [SecurityCritical]
  35. public static StreamReader OpenText(string path)
  36. {
  37. return new StreamReader(OpenRead(path), NativeMethods.DefaultFileEncoding);
  38. }
  39. /// <summary>[AlphaFS] Opens an existing UTF-8 encoded text file for reading.</summary>
  40. /// <param name="path">The file to be opened for reading.</param>
  41. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  42. /// <returns>A <see cref="StreamReader"/> on the specified path.</returns>
  43. /// <remarks>This method is equivalent to the <see cref="StreamReader"/>(String) constructor overload.</remarks>
  44. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  45. [SecurityCritical]
  46. public static StreamReader OpenText(string path, PathFormat pathFormat)
  47. {
  48. return new StreamReader(OpenRead(path, pathFormat), NativeMethods.DefaultFileEncoding);
  49. }
  50. /// <summary>[AlphaFS] Opens an existing <see cref="Encoding"/> encoded text file for reading.</summary>
  51. /// <param name="path">The file to be opened for reading.</param>
  52. /// <param name="encoding">The <see cref="Encoding"/> applied to the contents of the file.</param>
  53. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  54. /// <returns>A <see cref="StreamReader"/> on the specified path.</returns>
  55. /// <remarks>This method is equivalent to the <see cref="StreamReader"/>(String) constructor overload.</remarks>
  56. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  57. [SecurityCritical]
  58. public static StreamReader OpenText(string path, Encoding encoding, PathFormat pathFormat)
  59. {
  60. return new StreamReader(OpenRead(path, pathFormat), encoding);
  61. }
  62. /// <summary>[AlphaFS] Opens an existing <see cref="Encoding"/> encoded text file for reading.</summary>
  63. /// <param name="path">The file to be opened for reading.</param>
  64. /// <param name="encoding">The <see cref="Encoding"/> applied to the contents of the file.</param>
  65. /// <returns>A <see cref="StreamReader"/> on the specified path.</returns>
  66. /// <remarks>This method is equivalent to the <see cref="StreamReader"/>(String) constructor overload.</remarks>
  67. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  68. [SecurityCritical]
  69. public static StreamReader OpenText(string path, Encoding encoding)
  70. {
  71. return new StreamReader(OpenRead(path), encoding);
  72. }
  73. /// <summary>[AlphaFS] Opens an existing UTF-8 encoded text file for reading.</summary>
  74. /// <param name="transaction">The transaction.</param>
  75. /// <param name="path">The file to be opened for reading.</param>
  76. /// <returns>A <see cref="StreamReader"/> on the specified path.</returns>
  77. /// <remarks>This method is equivalent to the <see cref="StreamReader"/>(String) constructor overload.</remarks>
  78. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  79. public static StreamReader OpenTextTransacted(KernelTransaction transaction, string path)
  80. {
  81. return new StreamReader(OpenReadTransacted(transaction, path), NativeMethods.DefaultFileEncoding);
  82. }
  83. /// <summary>[AlphaFS] Opens an existing UTF-8 encoded text file for reading.</summary>
  84. /// <param name="transaction">The transaction.</param>
  85. /// <param name="path">The file to be opened for reading.</param>
  86. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  87. /// <returns>A <see cref="StreamReader"/> on the specified path.</returns>
  88. /// <remarks>This method is equivalent to the <see cref="StreamReader"/>(String) constructor overload.</remarks>
  89. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  90. public static StreamReader OpenTextTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  91. {
  92. return new StreamReader(OpenReadTransacted(transaction, path, pathFormat), NativeMethods.DefaultFileEncoding);
  93. }
  94. /// <summary>[AlphaFS] Opens an existing <see cref="Encoding"/> encoded text file for reading.</summary>
  95. /// <param name="transaction">The transaction.</param>
  96. /// <param name="path">The file to be opened for reading.</param>
  97. /// <param name="encoding">The <see cref="Encoding"/> applied to the contents of the file.</param>
  98. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  99. /// <returns>A <see cref="StreamReader"/> on the specified path.</returns>
  100. /// <remarks>This method is equivalent to the <see cref="StreamReader"/>(String) constructor overload.</remarks>
  101. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  102. public static StreamReader OpenTextTransacted(KernelTransaction transaction, string path, Encoding encoding, PathFormat pathFormat)
  103. {
  104. return new StreamReader(OpenReadTransacted(transaction, path, pathFormat), encoding);
  105. }
  106. /// <summary>[AlphaFS] Opens an existing <see cref="Encoding"/> encoded text file for reading.</summary>
  107. /// <param name="transaction">The transaction.</param>
  108. /// <param name="path">The file to be opened for reading.</param>
  109. /// <param name="encoding">The <see cref="Encoding"/> applied to the contents of the file.</param>
  110. /// <returns>A <see cref="StreamReader"/> on the specified path.</returns>
  111. /// <remarks>This method is equivalent to the <see cref="StreamReader"/>(String) constructor overload.</remarks>
  112. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  113. public static StreamReader OpenTextTransacted(KernelTransaction transaction, string path, Encoding encoding)
  114. {
  115. return new StreamReader(OpenReadTransacted(transaction, path), encoding);
  116. }
  117. }
  118. }