コード例 #1
0
ファイル: File.cs プロジェクト: peterdettman/coreclr
        /// <devdoc>
        ///    Note: This returns the fully qualified name of the destination file.
        /// </devdoc>
        internal static String InternalCopy(String sourceFileName, String destFileName, bool overwrite)
        {
            Contract.Requires(sourceFileName != null);
            Contract.Requires(destFileName != null);
            Contract.Requires(sourceFileName.Length > 0);
            Contract.Requires(destFileName.Length > 0);

            String fullSourceFileName = Path.GetFullPath(sourceFileName);
            String fullDestFileName   = Path.GetFullPath(destFileName);

            bool r = Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite);

            if (!r)
            {
                // Save Win32 error because subsequent checks will overwrite this HRESULT.
                int    errorCode = Marshal.GetLastWin32Error();
                String fileName  = destFileName;

                if (errorCode != Win32Native.ERROR_FILE_EXISTS)
                {
                    if (errorCode == Win32Native.ERROR_ACCESS_DENIED)
                    {
                        if (Directory.InternalExists(fullDestFileName))
                        {
                            throw new IOException(Environment.GetResourceString("Arg_FileIsDirectory_Name", destFileName), Win32Native.ERROR_ACCESS_DENIED, fullDestFileName);
                        }
                    }
                }

                __Error.WinIOError(errorCode, fileName);
            }

            return(fullDestFileName);
        }
コード例 #2
0
ファイル: file.cs プロジェクト: wolewd/Windows-Server-2003
        // Deletes a file. The file specified by the designated path is deleted.
        // If the file does not exist, Delete succeeds without throwing
        // an exception.
        //
        // On NT, Delete will fail for a file that is open for normal I/O
        // or a file that is memory mapped.  On Win95, the file will be
        // deleted irregardless of whether the file is being used.
        //
        // Your application must have Delete permission to the target file.
        //
        /// <include file='doc\File.uex' path='docs/doc[@for="File.Delete"]/*' />
        public static void Delete(String path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            String fullPath = Path.GetFullPathInternal(path);

            // For security check, path should be resolved to an absolute path.
            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { fullPath }, false, false).Demand();

            if (Directory.InternalExists(fullPath)) // Win9x hack to behave same as Winnt. Win9x fails silently for directories
            {
                throw new UnauthorizedAccessException(String.Format(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path"), path));
            }

            bool r = Win32Native.DeleteFile(fullPath);

            if (!r)
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    return;
                }
                else
                {
                    __Error.WinIOError(hr, path);
                }
            }
        }
コード例 #3
0
        internal static bool InternalExists(String path, out int lastError)
        {
            Contract.Requires(path != null);
            String tempPath = Path.AddLongPathPrefix(path);

            return(Directory.InternalExists(tempPath, out lastError));
        }
コード例 #4
0
        internal static string InternalCopy(string sourceFileName, string destFileName, bool overwrite)
        {
            string fullPathInternal = Path.GetFullPathInternal(sourceFileName);

            new FileIOPermission(FileIOPermissionAccess.Read, new string[] { fullPathInternal }, false, false).Demand();
            string dst = Path.GetFullPathInternal(destFileName);

            new FileIOPermission(FileIOPermissionAccess.Write, new string[] { dst }, false, false).Demand();
            if (!Win32Native.CopyFile(fullPathInternal, dst, !overwrite))
            {
                int    errorCode     = Marshal.GetLastWin32Error();
                string maybeFullPath = destFileName;
                if (errorCode != 80)
                {
                    using (SafeFileHandle handle = Win32Native.UnsafeCreateFile(fullPathInternal, -2147483648, FileShare.Read, null, FileMode.Open, 0, IntPtr.Zero))
                    {
                        if (handle.IsInvalid)
                        {
                            maybeFullPath = sourceFileName;
                        }
                    }
                    if ((errorCode == 5) && Directory.InternalExists(dst))
                    {
                        throw new IOException(Environment.GetResourceString("Arg_FileIsDirectory_Name", new object[] { destFileName }), 5, dst);
                    }
                }
                __Error.WinIOError(errorCode, maybeFullPath);
            }
            return(dst);
        }
コード例 #5
0
		/// <include file='doc\DirectoryInfo.uex' path='docs/doc[@for="DirectoryInfo.MoveTo"]/*' />
        public void MoveTo(String destDirName) {
            if (destDirName==null)
                throw new ArgumentNullException("destDirName");
            if (destDirName.Length==0)
                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destDirName");
    		
            new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, demandDir, false, false).Demand();
            String fullDestDirName = Path.GetFullPathInternal(destDirName);
            String demandPath;
            if (!fullDestDirName.EndsWith( '\\'))
                fullDestDirName = fullDestDirName + "\\";

            demandPath = fullDestDirName + ".";

            new FileIOPermission(FileIOPermissionAccess.Write, demandPath).Demand();

            String fullSourcePath;
            if (FullPath.EndsWith( '\\' ))
                fullSourcePath = FullPath;
            else
                fullSourcePath = FullPath + "\\";

            if (CultureInfo.InvariantCulture.CompareInfo.Compare(fullSourcePath, fullDestDirName, CompareOptions.IgnoreCase) == 0)
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustBeDifferent"));

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

            if (CultureInfo.InvariantCulture.CompareInfo.Compare(sourceRoot, destinationRoot, CompareOptions.IgnoreCase) != 0)
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustHaveSameRoot"));
    		
            if (!Directory.InternalExists(FullPath)) // Win9x hack to behave same as Winnt. 
                throw new DirectoryNotFoundException(String.Format(Environment.GetResourceString("IO.PathNotFound_Path"), destDirName));
    
    		
            if (!Win32Native.MoveFile(FullPath, destDirName))
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND) // Win32 is weird, it gives back a file not found
                {
                    hr = Win32Native.ERROR_PATH_NOT_FOUND;
                    __Error.WinIOError(hr, OriginalPath);
                }
				
                if (hr == Win32Native.ERROR_ACCESS_DENIED) // WinNT throws IOException. Win9x hack to do the same.
                    throw new IOException(String.Format(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path"), OriginalPath));
    		
                __Error.WinIOError(hr,String.Empty);
            }
            FullPath = fullDestDirName;
            OriginalPath = destDirName;
            if (FullPath.EndsWith( '\\' ))
                demandDir = new String[] { FullPath + "." };
            else
                demandDir = new String[] { FullPath + "\\." };

            // Flush any cached information about the directory.
            _dataInitialised = -1;
        }
コード例 #6
0
        internal static String InternalCopy(String sourceFileName, String destFileName, bool overwrite)
        {
            if (sourceFileName == null || destFileName == null)
            {
                throw new ArgumentNullException((sourceFileName == null ? "sourceFileName" : "destFileName"), Environment.GetResourceString("ArgumentNull_FileName"));
            }
            if (sourceFileName.Length == 0 || destFileName.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), (sourceFileName.Length == 0 ? "sourceFileName" : "destFileName"));
            }

            String fullSourceFileName = Path.GetFullPathInternal(sourceFileName);

            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullSourceFileName }, false, false).Demand();
            String fullDestFileName = Path.GetFullPathInternal(destFileName);

            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { fullDestFileName }, false, false).Demand();

            bool r = Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite);

            if (!r)
            {
                // Save Win32 error because subsequent checks will overwrite this HRESULT.
                int    errorCode = Marshal.GetLastWin32Error();
                String fileName  = destFileName;

                if (errorCode != Win32Native.ERROR_FILE_EXISTS)
                {
                    // For a number of error codes (sharing violation, path
                    // not found, etc) we don't know if the problem was with
                    // the source or dest file.  Try reading the source file.
                    using (SafeFileHandle handle = Win32Native.UnsafeCreateFile(fullSourceFileName, FileStream.GENERIC_READ, FileShare.Read, null, FileMode.Open, 0, IntPtr.Zero)) {
                        if (handle.IsInvalid)
                        {
                            fileName = sourceFileName;
                        }
                    }

                    if (errorCode == Win32Native.ERROR_ACCESS_DENIED)
                    {
                        if (Directory.InternalExists(fullDestFileName))
                        {
                            throw new IOException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Arg_FileIsDirectory_Name"), destFileName), Win32Native.ERROR_ACCESS_DENIED, fullDestFileName);
                        }
                    }
                }

                __Error.WinIOError(errorCode, fileName);
            }

            return(fullDestFileName);
        }
コード例 #7
0
ファイル: file.cs プロジェクト: wolewd/Windows-Server-2003
        /// <include file='doc\File.uex' path='docs/doc[@for="File.InternalCopy"]/*' />
        /// <devdoc>
        ///    Note: This returns the fully qualified name of the destination file.
        /// </devdoc>
        internal static String InternalCopy(String sourceFileName, String destFileName, bool overwrite)
        {
            if (sourceFileName == null || destFileName == null)
            {
                throw new ArgumentNullException((sourceFileName == null ? "sourceFileName" : "destFileName"), Environment.GetResourceString("ArgumentNull_FileName"));
            }
            if (sourceFileName.Length == 0 || destFileName.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), (sourceFileName.Length == 0 ? "sourceFileName" : "destFileName"));
            }

            String fullSourceFileName = Path.GetFullPathInternal(sourceFileName);

            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullSourceFileName }, false, false).Demand();
            String fullDestFileName = Path.GetFullPathInternal(destFileName);

            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { fullDestFileName }, false, false).Demand();

            bool r = Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite);

            if (!r)
            {
                // Save Win32 error because subsequent checks will overwrite this HRESULT.
                int hr = Marshal.GetLastWin32Error();

                if (hr != Win32Native.ERROR_FILE_EXISTS)
                {
                    if (!InternalExists(fullSourceFileName))
                    {
                        __Error.WinIOError(Win32Native.ERROR_FILE_NOT_FOUND, sourceFileName);
                    }

                    if (Directory.InternalExists(fullDestFileName))
                    {
                        throw new IOException(Environment.GetResourceString("Arg_DirExists"));
                    }
                }

                __Error.WinIOError(hr, destFileName);
            }

            return(fullDestFileName);
        }
コード例 #8
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");
            }

            new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, demandDir, false, false).Demand();
            String fullDestDirName = Path.GetFullPathInternal(destDirName);
            String demandPath;

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

            demandPath = fullDestDirName + '.';

            // 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.
            new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, demandPath).Demand();

            String fullSourcePath;

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

            if (CultureInfo.InvariantCulture.CompareInfo.Compare(fullSourcePath, fullDestDirName, CompareOptions.IgnoreCase) == 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustBeDifferent"));
            }

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

            if (CultureInfo.InvariantCulture.CompareInfo.Compare(sourceRoot, destinationRoot, CompareOptions.IgnoreCase) != 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustHaveSameRoot"));
            }

            if (Environment.IsWin9X() && !Directory.InternalExists(FullPath)) // For Win9x
            {
                throw new DirectoryNotFoundException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("IO.PathNotFound_Path"), destDirName));
            }


            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, OriginalPath);
                }

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

                __Error.WinIOError(hr, String.Empty);
            }
            FullPath     = fullDestDirName;
            OriginalPath = destDirName;
            demandDir    = new String[] { Directory.GetDemandDir(FullPath, true) };

            // Flush any cached information about the directory.
            _dataInitialised = -1;
        }
コード例 #9
0
ファイル: directory.cs プロジェクト: SSCLI/sscli_20021101
        /// <include file='doc\Directory.uex' path='docs/doc[@for="Directory.Move"]/*' />
        public static void Move(String sourceDirName, String destDirName)
        {
            if (sourceDirName == null)
            {
                throw new ArgumentNullException("sourceDirName");
            }
            if (sourceDirName.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "sourceDirName");
            }

            if (destDirName == null)
            {
                throw new ArgumentNullException("destDirName");
            }
            if (destDirName.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destDirName");
            }

            String fullsourceDirName = Path.GetFullPathInternal(sourceDirName);

            new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, new String[] { fullsourceDirName }, false, false).Demand();

            String fulldestDirName = Path.GetFullPathInternal(destDirName);

            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { fulldestDirName }, false, false).Demand();

            String sourcePath;

            if (fullsourceDirName.EndsWith(Path.DirectorySeparatorChar))
            {
                sourcePath = fullsourceDirName;
            }
            else
            {
                sourcePath = fullsourceDirName + Path.DirectorySeparatorChar;
            }

            String destPath;

            if (fulldestDirName.EndsWith(Path.DirectorySeparatorChar))
            {
                destPath = fulldestDirName;
            }
            else
            {
                destPath = fulldestDirName + Path.DirectorySeparatorChar;
            }

            if (CultureInfo.InvariantCulture.CompareInfo.Compare(sourcePath, destPath, CompareOptions.IgnoreCase) == 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustBeDifferent"));
            }

            String sourceRoot      = Path.GetPathRoot(sourcePath);
            String destinationRoot = Path.GetPathRoot(destPath);

            if (CultureInfo.InvariantCulture.CompareInfo.Compare(sourceRoot, destinationRoot, CompareOptions.IgnoreCase) != 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustHaveSameRoot"));
            }

            if (!Directory.InternalExists(Path.GetPathRoot(fulldestDirName))) // Win9x hack to behave same as Winnt.
            {
                throw new DirectoryNotFoundException(String.Format(Environment.GetResourceString("IO.PathNotFound_Path"), destDirName));
            }

            if (!Win32Native.MoveFile(sourceDirName, destDirName))
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND) // Win32 is weird, it gives back a file not found
                {
                    hr = Win32Native.ERROR_PATH_NOT_FOUND;
                    __Error.WinIOError(hr, sourceDirName);
                }
                if (hr == Win32Native.ERROR_ACCESS_DENIED) // WinNT throws IOException. Win9x hack to do the same.
                {
                    throw new IOException(String.Format(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path"), sourceDirName));
                }
                __Error.WinIOError(hr, String.Empty);
            }
        }
コード例 #10
0
 internal static bool InternalExists(string path, out int lastError)
 {
     return(Directory.InternalExists(Path.AddLongPathPrefix(path), out lastError));
 }