public int AsyncCopyFileUnbuffered(string inputfile, string outputfile, bool overwrite,bool movefile, bool checksum, int buffersize, bool reportprogress) {
//report write progress
_reportprogress = reportprogress;
//set file name globals
_inputfile = inputfile;
_outputfile = outputfile;
//setup single buffer size, remember this will be x3.
CopyBufferSize = buffersize * 1024 * 1024;
//buffer read
Buffer1 = new byte[CopyBufferSize];
//buffer overlap
Buffer2 = new byte[CopyBufferSize];
//buffer write
Buffer3 = new byte[CopyBufferSize];
//clear all flags and handles
_totalbytesread = 0;
_totalbyteswritten = 0;
_bytesRead1 = 0;
_buffer2Dirty = false;
//if the overwrite flag is set to false check to see if the file is there.
if (File.Exists(outputfile) && !overwrite) {
Console.WriteLine("Destination File Exists!");
return 0;
}
//create the directory if it doesn't exist
if (!Directory.Exists(outputfile)) {
try {
// ReSharper disable AssignNullToNotNullAttribute
Directory.CreateDirectory(Path.GetDirectoryName(outputfile));
// ReSharper restore AssignNullToNotNullAttribute
} catch (Exception e) {
Console.WriteLine("Create Directory Failed.");
Console.WriteLine(e.Message);
throw;
}
}
//get input file size for later use
var inputFileInfo = new FileInfo(_inputfile);
_infilesize = inputFileInfo.Length;
//get number of buffer sized chunks used to correctly display percent complete.
_numchunks = (int)((_infilesize / CopyBufferSize) <= 0 ? (_infilesize / CopyBufferSize) : 1);
Console.WriteLine("File Copy Started");
//create read thread and start it.
var readfile = new Thread(AsyncReadFile) { Name = "ReadThread", IsBackground = true };
readfile.Start();
//create write thread and start it.
var writefile = new Thread(AsyncWriteFile) { Name = "WriteThread", IsBackground = true };
writefile.Start();
if (_reportprogress) {
//set fancy curor position
_origRow = Console.CursorTop;
_origCol = Console.CursorLeft;
}
//wait for threads to finish
readfile.Join();
writefile.Join();
//leave a blank line for the progress indicator
if (_reportprogress)
Console.WriteLine();
Console.WriteLine("File Copy Done");
if (checksum) {
Console.WriteLine("Checksum Source File Started");
//create checksum read file thread and start it.
var checksumreadfile = new Thread(GetMD5HashFromInputFile) {
Name = "checksumreadfile",
IsBackground = true
};
checksumreadfile.Start();
Console.WriteLine("Checksum Destination File Started");
//create checksum write file thread and start it.
var checksumwritefile = new Thread(GetMD5HashFromOutputFile) {
Name = "checksumwritefile",
IsBackground = true
};
checksumwritefile.Start();
//hang out until the checksums are done.
checksumreadfile.Join();
checksumwritefile.Join();
if (_infilechecksum.Equals(_outfilechecksum)) {
Console.WriteLine("Checksum Verified");
} else {
Console.WriteLine("Checksum Failed");
Console.WriteLine("Input File Checksum : {0}", _infilechecksum);
Console.WriteLine("Output File Checksum: {0}", _outfilechecksum);
}
}
if (movefile && File.Exists(inputfile) && File.Exists(outputfile))
try {
File.Delete(inputfile);
} catch (IOException ioex) {
Console.WriteLine("File in use or locked");
Console.WriteLine(ioex.Message);
} catch (Exception ex) {
Console.WriteLine("File Failed to Delete");
Console.WriteLine(ex.Message);
}
return 1;
}