Example #1
0
        public void MoveTo(string destDirName)
        {
            if (destDirName == null)
            {
                throw new ArgumentNullException("destDirName");
            }
            if (destDirName.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destDirName");
            }
            Contract.EndContractBlock();

            Directory.CheckPermissions(DisplayPath, FullPath, checkHost: true, access: FileSecurityStateAccess.Write | FileSecurityStateAccess.Read);

            string fullDestDirName = Path.GetFullPathInternal(destDirName);

            if (!fullDestDirName.EndsWith(Path.DirectorySeparatorChar))
            {
                fullDestDirName = fullDestDirName + Path.DirectorySeparatorChar;
            }

            // Demand read & write permission to destination.  The reason is
            // we hand back a DirectoryInfo to the destination that would allow
            // you to read a directory listing from that directory.  Sure, you
            // had the ability to read the file contents in the old location,
            // but you technically also need read permissions to the new
            // location as well, and write is not a true superset of read.

            Directory.CheckPermissions(destDirName, fullDestDirName, checkHost: true, access: FileSecurityStateAccess.Write | FileSecurityStateAccess.Read);

            String fullSourcePath;

            if (FullPath.EndsWith(Path.DirectorySeparatorChar))
            {
                fullSourcePath = FullPath;
            }
            else
            {
                fullSourcePath = FullPath + Path.DirectorySeparatorChar;
            }

            if (String.Compare(fullSourcePath, fullDestDirName, StringComparison.OrdinalIgnoreCase) == 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustBeDifferent"));
            }

            String sourceRoot      = Path.GetPathRoot(fullSourcePath);
            String destinationRoot = Path.GetPathRoot(fullDestDirName);

            if (String.Compare(sourceRoot, destinationRoot, StringComparison.OrdinalIgnoreCase) != 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustHaveSameRoot"));
            }

            if (!Win32Native.MoveFile(FullPath, destDirName))
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND) // A dubious error code
                {
                    hr = Win32Native.ERROR_PATH_NOT_FOUND;
                    __Error.WinIOError(hr, DisplayPath);
                }

                if (hr == Win32Native.ERROR_ACCESS_DENIED) // We did this for Win9x. We can't change it for backcomp.
                {
                    throw new IOException(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path", DisplayPath));
                }

                __Error.WinIOError(hr, string.Empty);
            }
            FullPath     = fullDestDirName;
            OriginalPath = destDirName;
            DisplayPath  = GetDisplayName(OriginalPath, FullPath);

            // Flush any cached information about the directory.
            _dataInitialised = -1;
        }
Example #2
0
 [System.Security.SecurityCritical]  // auto-generated
 private DirectoryInfo(SerializationInfo info, StreamingContext context) : base(info, context)
 {
     Directory.CheckPermissions(string.Empty, FullPath, checkHost: false);
     DisplayPath = GetDisplayName(OriginalPath, FullPath);
 }