That is, a small segment of the file is copied each time to save the total memory overhead. Of course, local replication can also adopt the system. Io. file. COPY method in. net.
/// <Summary>
/// Copy an object
/// </Summary>
/// <Param name = "fromfile"> file to be copied </param>
/// <Param name = "tofile"> location to be saved </param>
/// <Param name = "lengtheachtime"> length of each copy </param>
Private void copyfile (string fromfile, string tofile, int lengtheachtime)
{
Filestream filetocopy = new filestream (fromfile, filemode. Open, fileaccess. Read );
Filestream copytofile = new filestream (tofile, filemode. append, fileaccess. Write );
Int lengthtocopy;
If (lengtheachtime <filetocopy. Length) // If multipart copy is performed, the content of each copy is smaller than the total length of the file.
{
Byte [] buffer = new byte [lengtheachtime];
Int copied = 0;
While (copied <= (INT) filetocopy. Length-lengtheachtime) // copy the subject part
{
Lengthtocopy = filetocopy. Read (buffer, 0, lengtheachtime );
Filetocopy. Flush ();
Copytofile. Write (buffer, 0, lengtheachtime );
Copytofile. Flush ();
Copytofile. Position = filetocopy. position;
Copied + = lengthtocopy;
}
Int left = (INT) filetocopy. Length-copied; // copy the remaining part
Lengthtocopy = filetocopy. Read (buffer, 0, left );
Filetocopy. Flush ();
Copytofile. Write (buffer, 0, left );
Copytofile. Flush ();
}
Else // if the overall copy, that is, the content of each copy is greater than the total length of the file
{
Byte [] buffer = new byte [filetocopy. Length];
Filetocopy. Read (buffer, 0, (INT) filetocopy. Length );
Filetocopy. Flush ();
Copytofile. Write (buffer, 0, (INT) filetocopy. Length );
Copytofile. Flush ();
}
Filetocopy. Close ();
Copytofile. Close ();
}