public static bool CopySource(string sourcefile, string copydirectory, ReportByte report, int buffersize = 4096) { if (!File.Exists(sourcefile)) { return(false); } try { string copy = Path.Combine(copydirectory, new FileInfo(sourcefile).Name); long total = new FileInfo(sourcefile).Length; FileStream fs1 = new FileStream(sourcefile, FileMode.Open, FileAccess.Read, FileShare.Read, buffersize); fs1.Seek(0, SeekOrigin.Begin); CopySection(fs1, copy, (int)total, buffersize, report); // Close the files. fs1.Close(); return(new FileInfo(sourcefile).Length == new FileInfo(copy).Length); } catch { return(false); } }
private static void CopySection(Stream input, string targetFile, int length, int buffersize, ReportByte report) { byte[] buffer = new byte[buffersize]; int total = length; using (Stream output = File.OpenWrite(targetFile)) { int bytesRead = 1; int totalBytes = 0; // This will finish silently if we couldn't read "length" bytes. // An alternative would be to throw an exception while (length > 0 && bytesRead > 0) { int fil = Math.Min(length, buffer.Length); bytesRead = input.Read(buffer, 0, fil); output.Write(buffer, 0, bytesRead); length -= bytesRead; totalBytes += fil; report(total, totalBytes); } } }