Esempio n. 1
0
        private static void InternalReplace(String sourceFileName, String destinationFileName, String destinationBackupFileName, bool ignoreMetadataErrors)
        {
            Contract.Requires(sourceFileName != null);
            Contract.Requires(destinationFileName != null);

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

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

            int flags = Win32Native.REPLACEFILE_WRITE_THROUGH;

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

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

            if (!r)
            {
                __Error.WinIOError();
            }
        }
Esempio n. 2
0
        public static void Replace(String sourceFileName, String destinationFileName, String destinationBackupFileName, bool ignoreMetadataErrors)
        {
            if (sourceFileName == null)
            {
                throw new ArgumentNullException("sourceFileName");
            }
            if (destinationFileName == null)
            {
                throw new ArgumentNullException("destinationFileName");
            }

            // Write permission to all three files, read permission to source
            // and dest.
            String fullSrcPath    = Path.GetFullPathInternal(sourceFileName);
            String fullDestPath   = Path.GetFullPathInternal(destinationFileName);
            String fullBackupPath = null;

            if (destinationBackupFileName != null)
            {
                fullBackupPath = Path.GetFullPathInternal(destinationBackupFileName);
            }
            FileIOPermission perm = new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, new String[] { fullSrcPath, fullDestPath });

            if (destinationBackupFileName != null)
            {
                perm.AddPathList(FileIOPermissionAccess.Write, fullBackupPath);
            }
            perm.Demand();


            int flags = Win32Native.REPLACEFILE_WRITE_THROUGH;

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

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

            if (!r)
            {
                __Error.WinIOError();
            }
        }
Esempio n. 3
0
        private static void InternalReplace(String sourceFileName, String destinationFileName, String destinationBackupFileName, bool ignoreMetadataErrors)
        {
            Contract.Requires(sourceFileName != null);
            Contract.Requires(destinationFileName != null);

            // Write permission to all three files, read permission to source
            // and dest.
            String fullSrcPath    = Path.GetFullPath(sourceFileName);
            String fullDestPath   = Path.GetFullPath(destinationFileName);
            String fullBackupPath = null;

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

            FileSecurityState sourceState = new FileSecurityState(FileSecurityStateAccess.Read | FileSecurityStateAccess.Write, sourceFileName, fullSrcPath);
            FileSecurityState destState   = new FileSecurityState(FileSecurityStateAccess.Read | FileSecurityStateAccess.Write, destinationFileName, fullDestPath);
            FileSecurityState backupState = new FileSecurityState(FileSecurityStateAccess.Read | FileSecurityStateAccess.Write, destinationBackupFileName, fullBackupPath);

            sourceState.EnsureState();
            destState.EnsureState();
            backupState.EnsureState();

            int flags = Win32Native.REPLACEFILE_WRITE_THROUGH;

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

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

            if (!r)
            {
                __Error.WinIOError();
            }
        }