Beispiel #1
0
        /// <summary>
        /// copy file or directory </summary>
        public static bool Copy(string SourcePath, string DestinationPath, CopyProgressDelegate callback = null)
        {
            try
            {
                if (Directory.Exists(SourcePath))
                {
                    foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))
                    {
                        Os.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
                    }

                    foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories))
                    {
                        CopyByBlock(newPath, newPath.Replace(SourcePath, DestinationPath), callback);
                    }
                }
                else if (File.Exists(SourcePath))
                {
                    CopyByBlock(SourcePath, Os.Combine(DestinationPath, Os.GetFileName(SourcePath)), callback);
                }
                return(true);
            }
            catch (Exception ex)
            {
                Program.log.Write(ex.Message);
                return(false);
            }
        }
Beispiel #2
0
 static public void CopyFile(
     string sourceFilename,
     string destinationFilename,
     CopyProgressDelegate progressHandler,
     IntPtr userData,
     CopyFileOptions copyOptions)
 {
     CopyFile(sourceFilename, destinationFilename, progressHandler, userData, copyOptions,
              CancellationToken.None);
 }
Beispiel #3
0
        public bool copyFile(string source, string destination, CancellableOperationProgressBase progress)
        {
            GCHandle handle      = GCHandle.Alloc(progress);
            IntPtr   progressPtr = GCHandle.ToIntPtr(handle);

            try
            {
                progress.ItemInfo = "Copying: " + source + " -> " + destination;

                FileIOPermission sourcePermission = new FileIOPermission(FileIOPermissionAccess.Read, source);
                sourcePermission.Demand();

                FileIOPermission destinationPermission = new FileIOPermission(
                    FileIOPermissionAccess.Write, destination);
                destinationPermission.Demand();

                string destinationDir = System.IO.Path.GetDirectoryName(destination);

                if (!Directory.Exists(destinationDir))
                {
                    Directory.CreateDirectory(destinationDir);
                }

                CopyProgressDelegate progressCallback = new CopyProgressDelegate(copyProgress);

                int cancel = 0;

                if (!CopyFileEx(source, destination, progressCallback,
                                progressPtr, ref cancel, (int)CopyFileOptions.ALL))
                {
                    Win32Exception win32exception = new Win32Exception();

                    if (win32exception.NativeErrorCode == 1235)
                    {
                        // copy was cancelled
                        return(false);
                    }

                    throw new IOException(win32exception.Message);
                }

                string info = "Copied: " + source + " -> " + destination;
                progress.InfoMessages.Add(info);
                Logger.Log.Info(info);

                return(true);
            }
            finally
            {
                handle.Free();
            }
        }
Beispiel #4
0
 public static void CopyByBlock(string inputPath, string outputPath, CopyProgressDelegate callback = null)
 {
     using (FileStream input = File.Open(inputPath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
         using (FileStream output = File.Open(outputPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
         {
             byte[] buffer = new byte[1024 * 1024];
             int    bytesRead;
             while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
             {
                 output.Write(buffer, 0, bytesRead);
                 callback?.Invoke(bytesRead);
             }
         }
     }
 }
Beispiel #5
0
        static private CopyFileResult CopyFileInternal(
            string sourceFilename,
            string destinationFilename,
            CopyProgressDelegate progressHandler,
            IntPtr userData,
            CopyFileOptions copyOptions,
            CancellationToken cancelToken)
        {
            // On error, throw IOException with the value from Marshal.GetLastWin32Error
            CopyProgressDelegate handler = progressHandler;
            int cancelFlag = 0;
            APICopyProgressRoutine callback;

            if (handler == null)
            {
                callback = null;
            }
            else
            {
                callback = new APICopyProgressRoutine((tfSize, xferBytes, strmSize, strmXferBytes,
                                                       strmNum, cbReason, srcHandle, dstHandle, udata) => {
                    var args = new CopyProgressArgs(tfSize, xferBytes, strmSize, strmXferBytes,
                                                    strmNum, cbReason, srcHandle, dstHandle, udata);
                    handler(args);
                    return((Int32)args.Result);
                });
            }
            if (cancelToken.CanBeCanceled)
            {
                cancelToken.Register(() => { cancelFlag = 1; });
            }
            bool rslt = CopyFileEx(
                sourceFilename,
                destinationFilename,
                callback,
                userData,
                ref cancelFlag,
                copyOptions);
            int err = 0;

            if (!rslt)
            {
                err = Marshal.GetLastWin32Error();
            }
            return(new CopyFileResult(rslt, err));
        }
Beispiel #6
0
        static public IAsyncResult BeginCopyFile(
            string sourceFilename,
            string destinationFilename,
            CopyProgressDelegate progressHandler,
            IntPtr userData,
            CopyFileOptions copyOptions,
            CancellationToken cancelToken,
            AsyncCallback callback = null)
        {
            var caller = new CopyFileInvoker(CopyFileInternal);

            return(caller.BeginInvoke(
                       sourceFilename,
                       destinationFilename,
                       progressHandler,
                       userData,
                       copyOptions,
                       cancelToken,
                       callback,
                       null));
        }
Beispiel #7
0
        static public void CopyFile(
            string sourceFilename,
            string destinationFilename,
            CopyProgressDelegate progressHandler,
            IntPtr userData,
            CopyFileOptions copyOptions,
            CancellationToken cancelToken)
        {
            var rslt = CopyFileInternal(
                sourceFilename,
                destinationFilename,
                progressHandler,
                userData,
                copyOptions,
                cancelToken);

            if (!rslt.ReturnValue)
            {
                throw new IOException(string.Format("Error copying file. GetLastError returns {0}.", rslt.LastError),
                                      rslt.LastError);
            }
        }
Beispiel #8
0
 static extern bool CopyFileEx(
     string lpExistingFileName, string lpgcnewFileName,
     CopyProgressDelegate lpProgressRoutine,
     IntPtr lpData, ref int pbCancel, int dwCopyFlags);
Beispiel #9
0
        public bool copyFile(string source, string destination, CancellableOperationProgressBase progress)
        {

            GCHandle handle = GCHandle.Alloc(progress);
            IntPtr progressPtr = GCHandle.ToIntPtr(handle);

            try
            {

                progress.ItemInfo = "Copying: " + source + " -> " + destination;

                FileIOPermission sourcePermission = new FileIOPermission(FileIOPermissionAccess.Read, source);
                sourcePermission.Demand();

                FileIOPermission destinationPermission = new FileIOPermission(
                    FileIOPermissionAccess.Write, destination);
                destinationPermission.Demand();

                string destinationDir = System.IO.Path.GetDirectoryName(destination);

                if (!Directory.Exists(destinationDir))
                {

                    Directory.CreateDirectory(destinationDir);                  
                }

                CopyProgressDelegate progressCallback = new CopyProgressDelegate(copyProgress);

                int cancel = 0;

                if (!CopyFileEx(source, destination, progressCallback,
                    progressPtr, ref cancel, (int)CopyFileOptions.ALL))
                {

                    Win32Exception win32exception = new Win32Exception();

                    if (win32exception.NativeErrorCode == 1235)
                    {

                        // copy was cancelled
                        return (false);
                    }

                    throw new IOException(win32exception.Message);
                }

                string info = "Copied: " + source + " -> " + destination;
                progress.InfoMessages.Add(info);
                Logger.Log.Info(info);

                return (true);

            }
            finally
            {

                handle.Free();
            }
        }
Beispiel #10
0
 static extern bool CopyFileEx(
     string lpExistingFileName, string lpgcnewFileName,
     CopyProgressDelegate lpProgressRoutine,
     IntPtr lpData, ref int pbCancel, int dwCopyFlags);
Beispiel #11
0
 public static extern bool CopyFileEx(string lpExistingFileName,
                                      string lpNewFileName,
                                      CopyProgressDelegate lpProgressRoutine,
                                      IntPtr lpData,
                                      [In] ref bool pbCancel,
                                      uint dwCopyFlags);
Beispiel #12
0
 public static extern bool CopyFileEx(string lpExistingFileName,
     string lpNewFileName,
     CopyProgressDelegate lpProgressRoutine,
     IntPtr lpData,
     [In] ref bool pbCancel,
     uint dwCopyFlags);