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.
 
 

120 lines
5.6 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;
  22. using System.Diagnostics.CodeAnalysis;
  23. using System.IO;
  24. using System.Security;
  25. using FileStream = System.IO.FileStream;
  26. namespace Alphaleonis.Win32.Filesystem
  27. {
  28. public static partial class File
  29. {
  30. #region WriteAllBytes
  31. /// <summary>
  32. /// Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is
  33. /// overwritten.
  34. /// </summary>
  35. /// <param name="path">The file to write to.</param>
  36. /// <param name="bytes">The bytes to write to the file.</param>
  37. [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "bytes")]
  38. [SecurityCritical]
  39. public static void WriteAllBytes(string path, byte[] bytes)
  40. {
  41. WriteAllBytesCore(null, path, bytes, PathFormat.RelativePath);
  42. }
  43. /// <summary>
  44. /// [AlphaFS] Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already
  45. /// exists, it is overwritten.
  46. /// </summary>
  47. /// <param name="path">The file to write to.</param>
  48. /// <param name="bytes">The bytes to write to the file.</param>
  49. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  50. [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "bytes")]
  51. [SecurityCritical]
  52. public static void WriteAllBytes(string path, byte[] bytes, PathFormat pathFormat)
  53. {
  54. WriteAllBytesCore(null, path, bytes, pathFormat);
  55. }
  56. #region Transactional
  57. /// <summary>
  58. /// [AlphaFS] Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already
  59. /// exists, it is overwritten.
  60. /// </summary>
  61. /// <param name="transaction">The transaction.</param>
  62. /// <param name="path">The file to write to.</param>
  63. /// <param name="bytes">The bytes to write to the file.</param>
  64. [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "bytes")]
  65. [SecurityCritical]
  66. public static void WriteAllBytesTransacted(KernelTransaction transaction, string path, byte[] bytes)
  67. {
  68. WriteAllBytesCore(transaction, path, bytes, PathFormat.RelativePath);
  69. }
  70. /// <summary>
  71. /// [AlphaFS] Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already
  72. /// exists, it is overwritten.
  73. /// </summary>
  74. /// <param name="transaction">The transaction.</param>
  75. /// <param name="path">The file to write to.</param>
  76. /// <param name="bytes">The bytes to write to the file.</param>
  77. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  78. [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "bytes")]
  79. [SecurityCritical]
  80. public static void WriteAllBytesTransacted(KernelTransaction transaction, string path, byte[] bytes, PathFormat pathFormat)
  81. {
  82. WriteAllBytesCore(transaction, path, bytes, pathFormat);
  83. }
  84. #endregion // Transacted
  85. #endregion // WriteAllBytes
  86. #region Internal Methods
  87. /// <summary>Creates a new file as part of a transaction, writes the specified byte array to
  88. /// the file, and then closes the file. If the target file already exists, it is overwritten.
  89. /// </summary>
  90. /// <exception cref="ArgumentNullException"/>
  91. /// <param name="transaction">The transaction.</param>
  92. /// <param name="path">The file to write to.</param>
  93. /// <param name="bytes">The bytes to write to the file.</param>
  94. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  95. [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "bytes")]
  96. [SecurityCritical]
  97. internal static void WriteAllBytesCore(KernelTransaction transaction, string path, byte[] bytes, PathFormat pathFormat)
  98. {
  99. if (bytes == null)
  100. throw new ArgumentNullException("bytes");
  101. using (FileStream fs = OpenCore(transaction, path, FileMode.Create, FileAccess.Write, FileShare.Read, ExtendedFileAttributes.Normal, null, null, pathFormat))
  102. fs.Write(bytes, 0, bytes.Length);
  103. }
  104. #endregion // Internal Methods
  105. }
  106. }