using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Copylockedfile
{
public static class FileHelper
{
public static bool CopyFile(string fromPath, string toPath, int eachReadLength)
{
if (File.Exists(toPath))
{
File.Delete(toPath);
}
FileStream fromFile = new FileStream(fromPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
FileStream toFile = new FileStream(toPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
int toCopyLength = 0;
if (eachReadLength < fromFile.Length)
{
byte[] buffer = new byte[eachReadLength];
long copied = 0;
while (copied <= fromFile.Length - eachReadLength)
{
toCopyLength = fromFile.Read(buffer, 0, eachReadLength);
fromFile.Flush();
toFile.Write(buffer, 0, eachReadLength);
toFile.Flush();
toFile.Position = fromFile.Position;
copied += toCopyLength;
}
int left = (int)(fromFile.Length - copied);
toCopyLength = fromFile.Read(buffer, 0, left);
fromFile.Flush();
toFile.Write(buffer, 0, left);
toFile.Flush();
}
else
{
byte[] buffer = new byte[fromFile.Length];
fromFile.Read(buffer, 0, buffer.Length);
fromFile.Flush();
toFile.Write(buffer, 0, buffer.Length);
toFile.Flush();
}
fromFile.Close();
toFile.Close();
return true;
}
}
}