Exemple #1
0
        public static void Move(string sourceDirName, string destDirName)
        {
            if (sourceDirName == null)
            {
                throw new ArgumentNullException("sourceDirName");
            }

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

            if (sourceDirName.Trim().Length == 0 || sourceDirName.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("Invalid source directory name: " + sourceDirName, "sourceDirName");
            }

            if (destDirName.Trim().Length == 0 || destDirName.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("Invalid target directory name: " + destDirName, "destDirName");
            }

            if (sourceDirName == destDirName)
            {
                throw new IOException("Source and destination path must be different.");
            }

            SecurityManager.EnsureElevatedPermissions();              // this is a no-op outside moonlight

            if (Exists(destDirName))
            {
                throw new IOException(destDirName + " already exists.");
            }

            if (!Exists(sourceDirName) && !File.Exists(sourceDirName))
            {
                throw new DirectoryNotFoundException(sourceDirName + " does not exist");
            }

            MonoIOError error;

            if (!MonoIO.MoveFile(sourceDirName, destDirName, out error))
            {
                throw MonoIO.GetException(error);
            }
        }
Exemple #2
0
        public void MoveTo(String destFileName)
        {
            if (destFileName == null)
            {
                throw new ArgumentNullException("destFileName");
            }
            if (destFileName.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName");
            }
            Contract.EndContractBlock();

            String fullDestFileName = Path.GetFullPathInternal(destFileName);

#if !MONO
#if FEATURE_CORECLR
            FileSecurityState sourceState = new FileSecurityState(FileSecurityStateAccess.Write | FileSecurityStateAccess.Read, DisplayPath, FullPath);
            FileSecurityState destState   = new FileSecurityState(FileSecurityStateAccess.Write, destFileName, fullDestFileName);
            sourceState.EnsureState();
            destState.EnsureState();
#else
            FileIOPermission.QuickDemand(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, FullPath, false, false);
            FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, fullDestFileName, false, false);
#endif
#endif

#if MONO
            MonoIOError error;
            if (!MonoIO.MoveFile(FullPath, fullDestFileName, out error))
            {
                __Error.WinIOError((int)error, String.Empty);
            }
#else
            if (!Win32Native.MoveFile(FullPath, fullDestFileName))
            {
                __Error.WinIOError();
            }
#endif
            FullPath     = fullDestFileName;
            OriginalPath = destFileName;
            _name        = Path.GetFileName(fullDestFileName);
            DisplayPath  = GetDisplayPath(destFileName);
            // Flush any cached information about the file.
            _dataInitialised = -1;
        }
Exemple #3
0
        public static void Move(string sourceFileName, string destFileName)
        {
            if (sourceFileName == null)
            {
                throw new ArgumentNullException("sourceFileName");
            }
            if (destFileName == null)
            {
                throw new ArgumentNullException("destFileName");
            }
            if (sourceFileName.Length == 0)
            {
                throw new ArgumentException("An empty file name is not valid.", "sourceFileName");
            }
            if (sourceFileName.Trim().Length == 0 || sourceFileName.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("The file name is not valid.");
            }
            if (destFileName.Length == 0)
            {
                throw new ArgumentException("An empty file name is not valid.", "destFileName");
            }
            if (destFileName.Trim().Length == 0 || destFileName.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("The file name is not valid.");
            }

            SecurityManager.EnsureElevatedPermissions();              // this is a no-op outside moonlight

            MonoIOError error;

            if (!MonoIO.Exists(sourceFileName, out error))
            {
                throw new FileNotFoundException(Locale.GetText("{0} does not exist", sourceFileName), sourceFileName);
            }

            // Don't check for this error here to allow the runtime
            // to check if sourceFileName and destFileName are equal.
            // Comparing sourceFileName and destFileName is not enough.
            //if (MonoIO.Exists (destFileName, out error))
            //	throw new IOException (Locale.GetText ("{0} already exists", destFileName));

            string DirName;

            DirName = Path.GetDirectoryName(destFileName);
            if (DirName != String.Empty && !Directory.Exists(DirName))
            {
                throw new DirectoryNotFoundException(Locale.GetText("Could not find a part of the path."));
            }

            if (!MonoIO.MoveFile(sourceFileName, destFileName, out error))
            {
                if (error == MonoIOError.ERROR_ALREADY_EXISTS)
                {
                    throw MonoIO.GetException(error);
                }
                else if (error == MonoIOError.ERROR_SHARING_VIOLATION)
                {
                    throw MonoIO.GetException(sourceFileName, error);
                }

                throw MonoIO.GetException(error);
            }
        }