Ejemplo n.º 1
0
        private void NicelyStopAndCleanUp_Win32(Win32FileIO.WinFileIO copier)
        {
            copier.Close();
            string tempName = TempFor(this.To);

            if (File.Exists(tempName))
            {
                File.Delete(tempName);
            }
        }
Ejemplo n.º 2
0
        private void CopyItOurself(ref bool pause, TVRenameStats stats)
        {
            const int kArrayLength = 1 * 1024 * 1024;
            Byte[] dataArray = new Byte[kArrayLength];

            bool useWin32 = Version.OnWindows() && !Version.OnMono();

            Win32FileIO.WinFileIO copier = null;

            BinaryReader msr = null;
            BinaryWriter msw = null;

            try
            {
                long thisFileCopied = 0;
                long thisFileSize = this.SourceFileSize();

                string tempName = TempFor(this.To);
                if (File.Exists(tempName))
                    File.Delete(tempName);

                if (useWin32)
                {
                    copier = new Win32FileIO.WinFileIO(dataArray);
                    copier.OpenForReading(this.From.FullName);
                    copier.OpenForWriting(tempName);
                }
                else
                {
                    msr = new BinaryReader(new FileStream(this.From.FullName, FileMode.Open, FileAccess.Read));
                    msw = new BinaryWriter(new FileStream(tempName, FileMode.CreateNew));
                }

                for (;;)
                {
                    int n = useWin32 ? copier.ReadBlocks(kArrayLength) : msr.Read(dataArray, 0, kArrayLength);
                    if (n == 0)
                        break;

                    if (useWin32)
                    {
                        copier.WriteBlocks(n);
                    }
                    else
                    {
                        msw.Write(dataArray, 0, n);
                    }
                    thisFileCopied += n;

                    double pct = (thisFileSize != 0) ? (100.0 * thisFileCopied / thisFileSize) : this.Done ? 100 : 0;
                    if (pct > 100.0)
                        pct = 100.0;
                    this.PercentDone = pct;

                    while (pause)
                        System.Threading.Thread.Sleep(100);
                }

                if (useWin32)
                {
                    copier.Close();
                }
                else
                {
                    msr.Close();
                    msw.Close();
                }

                // rename temp version to final name
                if (this.To.Exists)
                    this.To.Delete(); // outta ma way!
                File.Move(tempName, this.To.FullName);

                KeepTimestamps(this.From, this.To);

                // if that was a move/rename, delete the source
                if (this.IsMoveRename())
                    this.From.Delete();

                if (this.Operation == ActionCopyMoveRename.Op.Move)
                    stats.FilesMoved++;
                else if (this.Operation == ActionCopyMoveRename.Op.Rename)
                    stats.FilesRenamed++;
                else if (this.Operation == ActionCopyMoveRename.Op.Copy)
                    stats.FilesCopied++;

                this.Done = true;
            } // try
            catch (System.Threading.ThreadAbortException)
            {
                if (useWin32)
                {
                    this.NicelyStopAndCleanUp_Win32(copier);
                }
                else
                {
                    this.NicelyStopAndCleanUp_Streams(msr, msw);
                }
                return;
            }
            catch (Exception ex)
            {
                // handle any exception type
                this.Error = true;
                this.Done = true;
                this.ErrorText = ex.Message;
                if (useWin32)
                {
                    this.NicelyStopAndCleanUp_Win32(copier);
                }
                else
                {
                    this.NicelyStopAndCleanUp_Streams(msr, msw);
                }
            }
        }
Ejemplo n.º 3
0
        private void CopyItOurself(ref bool pause, TVRenameStats stats)
        {
            const int kArrayLength = 1 * 1024 * 1024;

            Byte[] dataArray = new Byte[kArrayLength];

            bool useWin32 = Version.OnWindows() && !Version.OnMono();

            Win32FileIO.WinFileIO copier = null;

            BinaryReader msr = null;
            BinaryWriter msw = null;

            try
            {
                long thisFileCopied = 0;
                long thisFileSize   = this.SourceFileSize();

                string tempName = TempFor(this.To);
                if (File.Exists(tempName))
                {
                    File.Delete(tempName);
                }

                if (useWin32)
                {
                    copier = new Win32FileIO.WinFileIO(dataArray);
                    copier.OpenForReading(this.From.FullName);
                    copier.OpenForWriting(tempName);
                }
                else
                {
                    msr = new BinaryReader(new FileStream(this.From.FullName, FileMode.Open, FileAccess.Read));
                    msw = new BinaryWriter(new FileStream(tempName, FileMode.CreateNew));
                }

                for (;;)
                {
                    int n = useWin32 ? copier.ReadBlocks(kArrayLength) : msr.Read(dataArray, 0, kArrayLength);
                    if (n == 0)
                    {
                        break;
                    }

                    if (useWin32)
                    {
                        copier.WriteBlocks(n);
                    }
                    else
                    {
                        msw.Write(dataArray, 0, n);
                    }
                    thisFileCopied += n;

                    double pct = (thisFileSize != 0) ? (100.0 * thisFileCopied / thisFileSize) : this.Done ? 100 : 0;
                    if (pct > 100.0)
                    {
                        pct = 100.0;
                    }
                    this.PercentDone = pct;

                    while (pause)
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                }

                if (useWin32)
                {
                    copier.Close();
                }
                else
                {
                    msr.Close();
                    msw.Close();
                }

                // rename temp version to final name
                if (this.To.Exists)
                {
                    this.To.Delete(); // outta ma way!
                }
                File.Move(tempName, this.To.FullName);

                KeepTimestamps(this.From, this.To);

                // if that was a move/rename, delete the source
                if (this.IsMoveRename())
                {
                    this.From.Delete();
                }

                if (this.Operation == ActionCopyMoveRename.Op.Move)
                {
                    stats.FilesMoved++;
                }
                else if (this.Operation == ActionCopyMoveRename.Op.Rename)
                {
                    stats.FilesRenamed++;
                }
                else if (this.Operation == ActionCopyMoveRename.Op.Copy)
                {
                    stats.FilesCopied++;
                }

                this.Done = true;
            } // try
            catch (System.Threading.ThreadAbortException)
            {
                if (useWin32)
                {
                    this.NicelyStopAndCleanUp_Win32(copier);
                }
                else
                {
                    this.NicelyStopAndCleanUp_Streams(msr, msw);
                }
                return;
            }
            catch (Exception ex)
            {
                // handle any exception type
                this.Error     = true;
                this.Done      = true;
                this.ErrorText = ex.Message;
                if (useWin32)
                {
                    this.NicelyStopAndCleanUp_Win32(copier);
                }
                else
                {
                    this.NicelyStopAndCleanUp_Streams(msr, msw);
                }
            }
        }