Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

111 lignes
6.0 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 StreamWriter = System.IO.StreamWriter;
  26. namespace Alphaleonis.Win32.Filesystem
  27. {
  28. public static partial class File
  29. {
  30. #region Public Methods
  31. /// <summary>Creates or opens a file for writing UTF-8 encoded text.</summary>
  32. /// <param name="path">The file to be opened for writing.</param>
  33. /// <returns>A StreamWriter that writes to the specified file using UTF-8 encoding.</returns>
  34. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  35. [SecurityCritical]
  36. public static StreamWriter CreateText(string path)
  37. {
  38. return CreateTextCore(null, path, NativeMethods.DefaultFileEncoding, PathFormat.RelativePath);
  39. }
  40. /// <summary>[AlphaFS] Creates or opens a file for writing UTF-8 encoded text.</summary>
  41. /// <param name="path">The file to be opened for writing.</param>
  42. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  43. /// <returns>A StreamWriter that writes to the specified file using UTF-8 encoding.</returns>
  44. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  45. [SecurityCritical]
  46. public static StreamWriter CreateText(string path, PathFormat pathFormat)
  47. {
  48. return CreateTextCore(null, path, NativeMethods.DefaultFileEncoding, pathFormat);
  49. }
  50. /// <summary>[AlphaFS] Creates or opens a file for writing <see cref="Encoding"/> encoded text.</summary>
  51. /// <param name="path">The file to be opened for writing.</param>
  52. /// <param name="encoding">The encoding that is applied to the contents of the file.</param>
  53. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  54. /// <returns>A StreamWriter that writes to the specified file using UTF-8 encoding.</returns>
  55. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  56. [SecurityCritical]
  57. public static StreamWriter CreateText(string path, Encoding encoding, PathFormat pathFormat)
  58. {
  59. return CreateTextCore(null, path, encoding, pathFormat);
  60. }
  61. /// <summary>[AlphaFS] Creates or opens a file for writing UTF-8 encoded text.</summary>
  62. /// <param name="transaction">The transaction.</param>
  63. /// <param name="path">The file to be opened for writing.</param>
  64. /// <returns>A StreamWriter that writes to the specified file using UTF-8 encoding.</returns>
  65. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  66. [SecurityCritical]
  67. public static StreamWriter CreateTextTransacted(KernelTransaction transaction, string path)
  68. {
  69. return CreateTextCore(transaction, path, NativeMethods.DefaultFileEncoding, PathFormat.RelativePath);
  70. }
  71. /// <summary>[AlphaFS] Creates or opens a file for writing <see cref="Encoding"/> encoded text.</summary>
  72. /// <param name="transaction">The transaction.</param>
  73. /// <param name="path">The file to be opened for writing.</param>
  74. /// <param name="encoding">The encoding that is applied to the contents of the file.</param>
  75. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  76. /// <returns>A StreamWriter that writes to the specified file using UTF-8 encoding.</returns>
  77. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  78. [SecurityCritical]
  79. public static StreamWriter CreateTextTransacted(KernelTransaction transaction, string path, Encoding encoding, PathFormat pathFormat)
  80. {
  81. return CreateTextCore(transaction, path, encoding, pathFormat);
  82. }
  83. #endregion
  84. #region Internal Methods
  85. /// <summary>Creates or opens a file for writing <see cref="Encoding"/> encoded text.</summary>
  86. /// <param name="transaction">The transaction.</param>
  87. /// <param name="path">The file to be opened for writing.</param>
  88. /// <param name="encoding">The <see cref="Encoding"/> applied to the contents of the file.</param>
  89. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  90. /// <returns>A <see cref="StreamWriter"/> that writes to the specified file using NativeMethods.DefaultFileBufferSize encoding.</returns>
  91. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  92. [SecurityCritical]
  93. internal static StreamWriter CreateTextCore(KernelTransaction transaction, string path, Encoding encoding, PathFormat pathFormat)
  94. {
  95. return new StreamWriter(CreateFileStreamCore(transaction, path, ExtendedFileAttributes.SequentialScan, null, FileMode.Create, FileAccess.Write, FileShare.Read, NativeMethods.DefaultFileBufferSize, pathFormat), encoding);
  96. }
  97. #endregion // Internal Methods
  98. }
  99. }