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.
 
 

195 regels
9.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.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. #region AppendText
  30. /// <summary>
  31. /// Creates a <see cref="StreamWriter"/> that appends NativeMethods.DefaultFileEncoding encoded text to an existing file, or to a new
  32. /// file if the specified file does not exist.
  33. /// </summary>
  34. /// <param name="path">The path to the file to append to.</param>
  35. /// <returns>
  36. /// A stream writer that appends NativeMethods.DefaultFileEncoding encoded text to the specified file or to a new file.
  37. /// </returns>
  38. [SecurityCritical]
  39. public static StreamWriter AppendText(string path)
  40. {
  41. return AppendTextCore(null, path, NativeMethods.DefaultFileEncoding, PathFormat.RelativePath);
  42. }
  43. /// <summary>
  44. /// Creates a <see cref="StreamWriter"/> that appends NativeMethods.DefaultFileEncoding encoded text to an existing file, or
  45. /// to a new file if the specified file does not exist.
  46. /// </summary>
  47. /// <param name="path">The path to the file to append to.</param>
  48. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  49. /// <returns>
  50. /// A stream writer that appends NativeMethods.DefaultFileEncoding encoded text to the specified file or to a new file.
  51. /// </returns>
  52. [SecurityCritical]
  53. public static StreamWriter AppendText(string path, PathFormat pathFormat)
  54. {
  55. return AppendTextCore(null, path, NativeMethods.DefaultFileEncoding, pathFormat);
  56. }
  57. /// <summary>
  58. /// Creates a <see cref="StreamWriter"/> that appends NativeMethods.DefaultFileEncoding encoded text to an existing file, or
  59. /// to a new file if the specified file does not exist.
  60. /// </summary>
  61. /// <param name="path">The path to the file to append to.</param>
  62. /// <param name="encoding">The character <see cref="Encoding"/> to use.</param>
  63. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  64. /// <returns>
  65. /// A stream writer that appends NativeMethods.DefaultFileEncoding encoded text to the specified file or to a new file.
  66. /// </returns>
  67. [SecurityCritical]
  68. public static StreamWriter AppendText(string path, Encoding encoding, PathFormat pathFormat)
  69. {
  70. return AppendTextCore(null, path, encoding, pathFormat);
  71. }
  72. /// <summary>
  73. /// Creates a <see cref="StreamWriter"/> that appends NativeMethods.DefaultFileEncoding encoded text to an existing file, or
  74. /// to a new file if the specified file does not exist.
  75. /// </summary>
  76. /// <param name="path">The path to the file to append to.</param>
  77. /// <param name="encoding">The character <see cref="Encoding"/> to use.</param>
  78. /// <returns>
  79. /// A stream writer that appends NativeMethods.DefaultFileEncoding encoded text to the specified file or to a new file.
  80. /// </returns>
  81. [SecurityCritical]
  82. public static StreamWriter AppendText(string path, Encoding encoding)
  83. {
  84. return AppendTextCore(null, path, encoding, PathFormat.RelativePath);
  85. }
  86. #region Transactional
  87. /// <summary>
  88. /// Creates a <see cref="StreamWriter"/> that appends NativeMethods.DefaultFileEncoding encoded text to an existing file, or
  89. /// to a new file if the specified file does not exist.
  90. /// </summary>
  91. /// <param name="transaction">The transaction.</param>
  92. /// <param name="path">The path to the file to append to.</param>
  93. /// <returns>
  94. /// A stream writer that appends NativeMethods.DefaultFileEncoding encoded text to the specified file or to a new file.
  95. /// </returns>
  96. [SecurityCritical]
  97. public static StreamWriter AppendTextTransacted(KernelTransaction transaction, string path)
  98. {
  99. return AppendTextCore(transaction, path, NativeMethods.DefaultFileEncoding, PathFormat.RelativePath);
  100. }
  101. /// <summary>
  102. /// Creates a <see cref="StreamWriter"/> that appends NativeMethods.DefaultFileEncoding encoded text to an existing file, or
  103. /// to a new file if the specified file does not exist.
  104. /// </summary>
  105. /// <param name="transaction">The transaction.</param>
  106. /// <param name="path">The path to the file to append to.</param>
  107. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  108. /// <returns>
  109. /// A stream writer that appends NativeMethods.DefaultFileEncoding encoded text to the specified file or to a new file.
  110. /// </returns>
  111. [SecurityCritical]
  112. public static StreamWriter AppendTextTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  113. {
  114. return AppendTextCore(transaction, path, NativeMethods.DefaultFileEncoding, pathFormat);
  115. }
  116. /// <summary>
  117. /// Creates a <see cref="StreamWriter"/> that appends NativeMethods.DefaultFileEncoding encoded text to an existing file, or
  118. /// to a new file if the specified file does not exist.
  119. /// </summary>
  120. /// <param name="transaction">The transaction.</param>
  121. /// <param name="path">The path to the file to append to.</param>
  122. /// <param name="encoding">The character <see cref="Encoding"/> to use.</param>
  123. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  124. /// <returns>
  125. /// A stream writer that appends NativeMethods.DefaultFileEncoding encoded text to the specified file or to a new file.
  126. /// </returns>
  127. [SecurityCritical]
  128. public static StreamWriter AppendTextTransacted(KernelTransaction transaction, string path, Encoding encoding, PathFormat pathFormat)
  129. {
  130. return AppendTextCore(transaction, path, encoding, pathFormat);
  131. }
  132. /// <summary>
  133. /// Creates a <see cref="StreamWriter"/> that appends NativeMethods.DefaultFileEncoding encoded text to an existing file, or
  134. /// to a new file if the specified file does not exist.
  135. /// </summary>
  136. /// <param name="transaction">The transaction.</param>
  137. /// <param name="path">The path to the file to append to.</param>
  138. /// <param name="encoding">The character <see cref="Encoding"/> to use.</param>
  139. /// <returns>
  140. /// A stream writer that appends NativeMethods.DefaultFileEncoding encoded text to the specified file or to a new file.
  141. /// </returns>
  142. [SecurityCritical]
  143. public static StreamWriter AppendTextTransacted(KernelTransaction transaction, string path, Encoding encoding)
  144. {
  145. return AppendTextCore(transaction, path, encoding, PathFormat.RelativePath);
  146. }
  147. #endregion // Transacted
  148. #endregion // AppendText
  149. #region Internal Methods
  150. /// <summary>Creates a <see cref="StreamWriter"/> that appends NativeMethods.DefaultFileEncoding encoded text to an existing file, or to a new file if the specified file does not exist.</summary>
  151. /// <exception cref="IOException"/>
  152. /// <param name="transaction">The transaction.</param>
  153. /// <param name="path">The path to the file to append to.</param>
  154. /// <param name="encoding">The character <see cref="Encoding"/> to use.</param>
  155. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  156. /// <returns>
  157. /// A stream writer that appends NativeMethods.DefaultFileEncoding encoded text to the specified file or to a new file.
  158. /// </returns>
  159. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  160. [SecurityCritical]
  161. internal static StreamWriter AppendTextCore(KernelTransaction transaction, string path, Encoding encoding, PathFormat pathFormat)
  162. {
  163. FileStream fs = OpenCore(transaction, path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, ExtendedFileAttributes.Normal, null, null, pathFormat);
  164. try
  165. {
  166. fs.Seek(0, SeekOrigin.End);
  167. return new StreamWriter(fs, encoding);
  168. }
  169. catch (IOException)
  170. {
  171. fs.Close();
  172. throw;
  173. }
  174. }
  175. #endregion // Internal Methods
  176. }
  177. }