Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

123 righe
9.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.Security;
  22. namespace Alphaleonis.Win32.Filesystem
  23. {
  24. partial class FileInfo
  25. {
  26. #region .NET
  27. /// <summary>Replaces the contents of a specified file with the file described by the current <see cref="FileInfo"/> object, deleting the original file, and creating a backup of the replaced file.</summary>
  28. /// <returns>A <see cref="FileInfo"/> object that encapsulates information about the file described by the <paramref name="destinationFileName"/> parameter.</returns>
  29. /// <remarks>
  30. /// The Replace method replaces the contents of a specified file with the contents of the file described by the current
  31. /// <see cref="FileInfo"/> object. It also creates a backup of the file that was replaced. Finally, it returns a new
  32. /// <see cref="FileInfo"/> object that describes the overwritten file.
  33. /// </remarks>
  34. /// <remarks>Pass null to the <paramref name="destinationBackupFileName"/> parameter if you do not want to create a backup of the file being replaced.</remarks>
  35. /// <param name="destinationFileName">The name of a file to replace with the current file.</param>
  36. /// <param name="destinationBackupFileName">The name of a file with which to create a backup of the file described by the <paramref name="destinationFileName"/> parameter.</param>
  37. [SecurityCritical]
  38. public FileInfo Replace(string destinationFileName, string destinationBackupFileName)
  39. {
  40. return Replace(destinationFileName, destinationBackupFileName, false, PathFormat.RelativePath);
  41. }
  42. /// <summary>Replaces the contents of a specified file with the file described by the current <see cref="FileInfo"/> object, deleting the original file, and creating a backup of the replaced file. Also specifies whether to ignore merge errors.</summary>
  43. /// <returns>A <see cref="FileInfo"/> object that encapsulates information about the file described by the <paramref name="destinationFileName"/> parameter.</returns>
  44. /// <remarks>
  45. /// The Replace method replaces the contents of a specified file with the contents of the file described by the current
  46. /// <see cref="FileInfo"/> object. It also creates a backup of the file that was replaced. Finally, it returns a new
  47. /// <see cref="FileInfo"/> object that describes the overwritten file.
  48. /// </remarks>
  49. /// <remarks>Pass null to the <paramref name="destinationBackupFileName"/> parameter if you do not want to create a backup of the file being replaced.</remarks>
  50. /// <param name="destinationFileName">The name of a file to replace with the current file.</param>
  51. /// <param name="destinationBackupFileName">The name of a file with which to create a backup of the file described by the <paramref name="destinationFileName"/> parameter.</param>
  52. /// <param name="ignoreMetadataErrors"><see langword="true"/> to ignore merge errors (such as attributes and ACLs) from the replaced file to the replacement file; otherwise, <see langword="false"/>.</param>
  53. [SecurityCritical]
  54. public FileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
  55. {
  56. return Replace(destinationFileName, destinationBackupFileName, ignoreMetadataErrors, PathFormat.RelativePath);
  57. }
  58. #endregion // .NET
  59. #region AlphaFS
  60. /// <summary>[AlphaFS] Replaces the contents of a specified file with the file described by the current <see cref="FileInfo"/> object, deleting the original file, and creating a backup of the replaced file. Also specifies whether to ignore merge errors.</summary>
  61. /// <returns>A <see cref="FileInfo"/> object that encapsulates information about the file described by the <paramref name="destinationFileName"/> parameter.</returns>
  62. /// <remarks>
  63. /// The Replace method replaces the contents of a specified file with the contents of the file described by the current
  64. /// <see cref="FileInfo"/> object. It also creates a backup of the file that was replaced. Finally, it returns a new
  65. /// <see cref="FileInfo"/> object that describes the overwritten file.
  66. /// </remarks>
  67. /// <remarks>Pass null to the <paramref name="destinationBackupFileName"/> parameter if you do not want to create a backup of the file being replaced.</remarks>
  68. /// <param name="destinationFileName">The name of a file to replace with the current file.</param>
  69. /// <param name="destinationBackupFileName">The name of a file with which to create a backup of the file described by the <paramref name="destinationFileName"/> parameter.</param>
  70. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  71. [SecurityCritical]
  72. public FileInfo Replace(string destinationFileName, string destinationBackupFileName, PathFormat pathFormat)
  73. {
  74. var options = GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck;
  75. string destinationFileNameLp = Path.GetExtendedLengthPathCore(Transaction, destinationFileName, pathFormat, options);
  76. string destinationBackupFileNameLp = destinationBackupFileName != null
  77. ? Path.GetExtendedLengthPathCore(Transaction, destinationBackupFileName, pathFormat, options)
  78. : null;
  79. File.ReplaceCore(LongFullName, destinationFileNameLp, destinationBackupFileNameLp, false, PathFormat.LongFullPath);
  80. return new FileInfo(Transaction, destinationFileNameLp, PathFormat.LongFullPath);
  81. }
  82. /// <summary>[AlphaFS] Replaces the contents of a specified file with the file described by the current <see cref="FileInfo"/> object, deleting the original file, and creating a backup of the replaced file. Also specifies whether to ignore merge errors.</summary>
  83. /// <returns>A <see cref="FileInfo"/> object that encapsulates information about the file described by the <paramref name="destinationFileName"/> parameter.</returns>
  84. /// <remarks>
  85. /// The Replace method replaces the contents of a specified file with the contents of the file described by the current
  86. /// <see cref="FileInfo"/> object. It also creates a backup of the file that was replaced. Finally, it returns a new
  87. /// <see cref="FileInfo"/> object that describes the overwritten file.
  88. /// </remarks>
  89. /// <remarks>Pass null to the <paramref name="destinationBackupFileName"/> parameter if you do not want to create a backup of the file being replaced.</remarks>
  90. /// <param name="destinationFileName">The name of a file to replace with the current file.</param>
  91. /// <param name="destinationBackupFileName">The name of a file with which to create a backup of the file described by the <paramref name="destinationFileName"/> parameter.</param>
  92. /// <param name="ignoreMetadataErrors"><see langword="true"/> to ignore merge errors (such as attributes and ACLs) from the replaced file to the replacement file; otherwise, <see langword="false"/>.</param>
  93. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  94. [SecurityCritical]
  95. public FileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors, PathFormat pathFormat)
  96. {
  97. var options = GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck;
  98. string destinationFileNameLp = Path.GetExtendedLengthPathCore(Transaction, destinationFileName, pathFormat, options);
  99. string destinationBackupFileNameLp = destinationBackupFileName != null
  100. ? Path.GetExtendedLengthPathCore(Transaction, destinationBackupFileName, pathFormat, options)
  101. : null;
  102. File.ReplaceCore(LongFullName, destinationFileNameLp, destinationBackupFileNameLp, ignoreMetadataErrors, PathFormat.LongFullPath);
  103. return new FileInfo(Transaction, destinationFileNameLp, PathFormat.LongFullPath);
  104. }
  105. #endregion // AlphaFS
  106. }
  107. }