Click here to Skip to main content
15,916,091 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

C#
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
Posted
Updated 25-Apr-16 22:52pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Apr-16 20:36pm    
This cannot be called "resuming". I could easily show how to get to the same position in the file, but it won't help you at all. The whole idea is wrong. Do you mean that the file is not found and then later it appears in space and you want to "continue" something? But if file was not found in first place, nothing was done. It all makes no sense at all.

So, why a file becomes inaccessible? Is it a source or destination file? Why are you not simply using File.Copy (yes, with full restart in case of failure)?

—SA
Sinisa Hajnal 25-Apr-16 6:46am    
I think he means the connection between source and destination is unstable and fails occasionally. That's how they become unavailable.

vast25, there are file operations in file namespace ad SA Kryukov suggests. Read about them, it may be easier then you think.
New_Yorker 25-Apr-16 9:23am    
You may use Position property of FileStream object both to read (and save) the last position you were at before connection was interrupted and to set the new position before resuming copying.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900