Exemple #1
0
        public static void Replace(String sourceFileName, String destinationFileName, String destinationBackupFileName,
                                   bool ignoreMetadataErrors)
        {
            if (Common.IsRunningOnMono() && Common.IsPlatformUnix())
            {
                SysFile.Replace(sourceFileName, destinationFileName, destinationBackupFileName, ignoreMetadataErrors);
                return;
            }

            if (sourceFileName == null)
            {
                throw new ArgumentNullException("sourceFileName");
            }
            if (destinationFileName == null)
            {
                throw new ArgumentNullException("destinationFileName");
            }

            String fullSrcPath    = Path.NormalizeLongPath(Path.GetFullPath(sourceFileName));
            String fullDestPath   = Path.NormalizeLongPath(Path.GetFullPath(destinationFileName));
            String fullBackupPath = null;

            if (destinationBackupFileName != null)
            {
                fullBackupPath = Path.NormalizeLongPath(Path.GetFullPath(destinationBackupFileName));
            }

            int flags = NativeMethods.REPLACEFILE_WRITE_THROUGH;

            if (ignoreMetadataErrors)
            {
                flags |= NativeMethods.REPLACEFILE_IGNORE_MERGE_ERRORS;
            }

            bool r = NativeMethods.ReplaceFile(fullDestPath, fullSrcPath, fullBackupPath, flags, IntPtr.Zero, IntPtr.Zero);

            if (!r)
            {
                Common.ThrowIOError(Marshal.GetLastWin32Error(), String.Empty);
            }
        }