private static string InternalCopy(string fullSourceFileName, string fullDestFileName, string sourceFileName, string destFileName, bool overwrite)
 {
     fullSourceFileName = Path.AddLongPathPrefix(fullSourceFileName);
     fullDestFileName   = Path.AddLongPathPrefix(fullDestFileName);
     if (!Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite))
     {
         int    errorCode     = Marshal.GetLastWin32Error();
         string maybeFullPath = destFileName;
         if (errorCode != 80)
         {
             using (SafeFileHandle handle = Win32Native.UnsafeCreateFile(fullSourceFileName, -2147483648, FileShare.Read, null, FileMode.Open, 0, IntPtr.Zero))
             {
                 if (handle.IsInvalid)
                 {
                     maybeFullPath = sourceFileName;
                 }
             }
             if ((errorCode == 5) && LongPathDirectory.InternalExists(fullDestFileName))
             {
                 throw new IOException(Environment.GetResourceString("Arg_FileIsDirectory_Name", new object[] { destFileName }), 5, fullDestFileName);
             }
         }
         __Error.WinIOError(errorCode, maybeFullPath);
     }
     return(fullDestFileName);
 }
Пример #2
0
 private static string InternalCopy(string fullSourceFileName, string fullDestFileName, string sourceFileName, string destFileName, bool overwrite)
 {
     fullSourceFileName = Path.AddLongPathPrefix(fullSourceFileName);
     fullDestFileName   = Path.AddLongPathPrefix(fullDestFileName);
     if (!Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite))
     {
         int    lastWin32Error = Marshal.GetLastWin32Error();
         string maybeFullPath  = destFileName;
         if (lastWin32Error != 80)
         {
             using (SafeFileHandle file = Win32Native.UnsafeCreateFile(fullSourceFileName, int.MinValue, FileShare.Read, (Win32Native.SECURITY_ATTRIBUTES)null, FileMode.Open, 0, IntPtr.Zero))
             {
                 if (file.IsInvalid)
                 {
                     maybeFullPath = sourceFileName;
                 }
             }
             if (lastWin32Error == 5 && LongPathDirectory.InternalExists(fullDestFileName))
             {
                 throw new IOException(Environment.GetResourceString("Arg_FileIsDirectory_Name", (object)destFileName), 5, fullDestFileName);
             }
         }
         __Error.WinIOError(lastWin32Error, maybeFullPath);
     }
     return(fullDestFileName);
 }
Пример #3
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);
        }
Пример #4
0
        public static string DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs, bool overwrite = true)
        {
            try
            {
                DirectoryInfo   dir  = new DirectoryInfo(sourceDirName);
                DirectoryInfo[] dirs = dir.GetDirectories();

                // If the source directory does not exist, throw an exception.
                if (!dir.Exists)
                {
                    throw new DirectoryNotFoundException(
                              "Source directory does not exist or could not be found: "
                              + sourceDirName);
                }

                // If the destination directory does not exist, create it.
                if (!Directory.Exists(destDirName))
                {
                    //Directory.CreateDirectory(destDirName);
                    var isCrtd = CreateDirectory(destDirName);
                    if (!isCrtd)
                    {
                        return("Directory could not created. Path = " + destDirName);
                    }
                }


                // Get the file contents of the directory to copy.
                FileInfo[] files = dir.GetFiles();

                foreach (FileInfo file in files)
                {
                    // Create the path to the new copy of the file.
                    string temppath = Path.Combine(destDirName, file.Name);

                    // Copy the file. Long file path problem
                    //file.CopyTo(temppath, overwrite);
                    Win32Native.CopyFile(file.FullName, temppath, !overwrite);
                }

                // If copySubDirs is true, copy the subdirectories.
                if (copySubDirs)
                {
                    foreach (DirectoryInfo subdir in dirs)
                    {
                        // Create the subdirectory.
                        string temppath = Path.Combine(destDirName, subdir.Name);

                        // Copy the subdirectories.
                        DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);

                return("FileOperationHelper.DirectoryCopy failed. ErrDetail = " + ex.ToString());
            }

            return(null);
        }