问题描述
我正在使用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
推荐答案
这篇关于如何恢复文件复制操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!