Exemple #1
0
        public static void Replace(string sourceFileName,
                                   string destinationFileName,
                                   string destinationBackupFileName,
                                   bool ignoreMetadataErrors)
        {
            MonoIOError error;

            if (sourceFileName == null)
            {
                throw new ArgumentNullException("sourceFileName");
            }
            if (destinationFileName == null)
            {
                throw new ArgumentNullException("destinationFileName");
            }
            if (sourceFileName.Trim().Length == 0 || sourceFileName.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("sourceFileName");
            }
            if (destinationFileName.Trim().Length == 0 || destinationFileName.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("destinationFileName");
            }

            string fullSource = Path.GetFullPath(sourceFileName);
            string fullDest   = Path.GetFullPath(destinationFileName);

            if (MonoIO.ExistsDirectory(fullSource, out error))
            {
                throw new IOException(Locale.GetText("{0} is a directory", sourceFileName));
            }
            if (MonoIO.ExistsDirectory(fullDest, out error))
            {
                throw new IOException(Locale.GetText("{0} is a directory", destinationFileName));
            }

            if (!Exists(fullSource))
            {
                throw new FileNotFoundException(Locale.GetText("{0} does not exist", sourceFileName),
                                                sourceFileName);
            }
            if (!Exists(fullDest))
            {
                throw new FileNotFoundException(Locale.GetText("{0} does not exist", destinationFileName),
                                                destinationFileName);
            }
            if (fullSource == fullDest)
            {
                throw new IOException(Locale.GetText("Source and destination arguments are the same file."));
            }

            string fullBackup = null;

            if (destinationBackupFileName != null)
            {
                if (destinationBackupFileName.Trim().Length == 0 ||
                    destinationBackupFileName.IndexOfAny(Path.InvalidPathChars) != -1)
                {
                    throw new ArgumentException("destinationBackupFileName");
                }

                fullBackup = Path.GetFullPath(destinationBackupFileName);
                if (MonoIO.ExistsDirectory(fullBackup, out error))
                {
                    throw new IOException(Locale.GetText("{0} is a directory", destinationBackupFileName));
                }
                if (fullSource == fullBackup)
                {
                    throw new IOException(Locale.GetText("Source and backup arguments are the same file."));
                }
                if (fullDest == fullBackup)
                {
                    throw new IOException(Locale.GetText(
                                              "Destination and backup arguments are the same file."));
                }
            }

            var attrs = GetAttributes(fullDest);

            // TODO: Should be done in wapi, win32 api handles this already
            if ((attrs & FileAttributes.ReadOnly) != 0)
            {
                throw MonoIO.GetException(MonoIOError.ERROR_ACCESS_DENIED);
            }

            if (!MonoIO.ReplaceFile(fullSource, fullDest, fullBackup,
                                    ignoreMetadataErrors, out error))
            {
                throw MonoIO.GetException(error);
            }
        }
Exemple #2
0
        public static void Replace(string sourceFileName,
                                   string destinationFileName,
                                   string destinationBackupFileName,
                                   bool ignoreMetadataErrors)
        {
            MonoIOError error;

            if (sourceFileName == null)
            {
                throw new ArgumentNullException("sourceFileName");
            }
            if (destinationFileName == null)
            {
                throw new ArgumentNullException("destinationFileName");
            }
            if (sourceFileName.Trim().Length == 0 || sourceFileName.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("sourceFileName");
            }
            if (destinationFileName.Trim().Length == 0 || destinationFileName.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("destinationFileName");
            }

            string fullSource = Path.GetFullPath(sourceFileName);
            string fullDest   = Path.GetFullPath(destinationFileName);

            if (MonoIO.ExistsDirectory(fullSource, out error))
            {
                throw new IOException(Locale.GetText("{0} is a directory", sourceFileName));
            }
            if (MonoIO.ExistsDirectory(fullDest, out error))
            {
                throw new IOException(Locale.GetText("{0} is a directory", destinationFileName));
            }

            if (!Exists(fullSource))
            {
                throw new FileNotFoundException(Locale.GetText("{0} does not exist", sourceFileName),
                                                sourceFileName);
            }
            if (!Exists(fullDest))
            {
                throw new FileNotFoundException(Locale.GetText("{0} does not exist", destinationFileName),
                                                destinationFileName);
            }
            if (fullSource == fullDest)
            {
                throw new IOException(Locale.GetText("Source and destination arguments are the same file."));
            }

            string fullBackup = null;

            if (destinationBackupFileName != null)
            {
                if (destinationBackupFileName.Trim().Length == 0 ||
                    destinationBackupFileName.IndexOfAny(Path.InvalidPathChars) != -1)
                {
                    throw new ArgumentException("destinationBackupFileName");
                }

                fullBackup = Path.GetFullPath(destinationBackupFileName);
                if (MonoIO.ExistsDirectory(fullBackup, out error))
                {
                    throw new IOException(Locale.GetText("{0} is a directory", destinationBackupFileName));
                }
                if (fullSource == fullBackup)
                {
                    throw new IOException(Locale.GetText("Source and backup arguments are the same file."));
                }
                if (fullDest == fullBackup)
                {
                    throw new IOException(Locale.GetText(
                                              "Destination and backup arguments are the same file."));
                }
            }

            if (!MonoIO.ReplaceFile(fullSource, fullDest, fullBackup,
                                    ignoreMetadataErrors, out error))
            {
                throw MonoIO.GetException(error);
            }
        }