如何恢复文件复制操作? [英] How do I resume my file copy operation?

查看:123
本文介绍了如何恢复文件复制操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用BinaryReader和BinaryWriter类来复制文件,但我想知道如何从上一个位置恢复文件复制操作。是否有任何可以跳过文件中现有字节的函数?

其实我需要这个,因为在文件复制期间如果源路径或目标路径变得不可访问,那么它会抛出路径未找到的异常,我必须重新启动操作以避免这种情况我可以如何恢复此操作?



我尝试了什么:



我正在尝试这段代码,但它会重启整个操作,因为我重新定义读者作家对象



I am using BinaryReader and BinaryWriter class to copy file but i want to know that how i could resume my file copy operation from last position.does there any function which can skip existing byte in the file?
Actually i need this because during file copy if source or destination path become inaccessible then it throw exception that path is not found and i have to restart the operation so to avoid this how i can resume this operation?

What I have tried:

I am trying this code but it restart whole operation because i m redefining reader writer object

if (!Directory.Exists(Path.GetPathRoot(SourceFileFullPath)))//if drive is not exist
            {

               binaryReader.Close();
                SourceFileFullPath = getNewPath("Source", SourceFileFullPath);

                 binaryReader = new BinaryReader(new FileStream(SourceFileFullPath, FileMode.Open, FileAccess.Read));//

            }
            else if (!Directory.Exists(Path.GetPathRoot(destinationPath)))//if drive is not exist
            {

 
                binaryWriter.Close();

                destinationPath = getNewPath("Destination", destinationPath);

                binaryWriter = new BinaryWriter(new FileStream(destinationPath, FileMode.Append, FileAccess.Write));

            }


            int data = binaryReader.Read(buf, 0, maxBuff);//read data
            if (data <= 0)//end of file
                break;
            if (data < maxBuff)
                maxBuff = (int)currentFileSizeInByteForProgressBar - (int)currentFileCopiedInByteForProgressBar;


            binaryWriter.Write(buf, 0, maxBuff);//write data

推荐答案

为什么不在复制后重命名文件是否成功表明它已被复制?



例如

步骤0)找到所有不以* _copied结尾的文件

步骤1)将文件复制到另一个目的地

步骤2)完成后将现有文件从aaa.txt重命名为aaa_copied.txt

步骤3)如果操作突然失败,则从步骤0恢复操作



希望它有所帮助。

*解决方案越简单,越少复杂性和易于调试。
Why not rename the file after copy is succeeded to indicate it has been copied?

e.g.
Step 0) Find all files that does not end with "*_copied"
Step 1) Copy files to another destination
Step 2) Rename existing file from "aaa.txt" to "aaa_copied.txt" when completed
Step 3) if operation suddenly fail, resume operation from step 0

Hope it helps.
*The simpler the solution, the less complexity and easier to debug.


这篇关于如何恢复文件复制操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆