Skip to content

Instantly share code, notes, and snippets.

@daveamato
Created May 24, 2014 00:12
Show Gist options
  • Save daveamato/acf2bebf922bef6346e9 to your computer and use it in GitHub Desktop.
Save daveamato/acf2bebf922bef6346e9 to your computer and use it in GitHub Desktop.
/// <summary> Fast file move with big buffers
/// </summary>
/// <param name="source">Source file path</param>
/// <param name="destination">Destination file path</param>
static void FMove (string source, string destination)
{
int array_length = (int) Math.Pow (2, 20);
byte[] dataArray = new byte[array_length];
FileStream fsread = new FileStream (source, FileMode.Open, FileAccess.Read, FileShare.None, array_length * 2);
BinaryReader bwread = new BinaryReader (fsread);
FileStream fswrite = new FileStream (destination, FileMode.Create, FileAccess.Write, FileShare.None, array_length * 2);
BinaryWriter bwwrite = new BinaryWriter (fswrite);
for (; ; )
{
int read = bwread.Read (dataArray, 0, array_length);
if (0 == read)
break;
bwwrite.Write (dataArray, 0, read);
}
bwwrite.Close ();
fswrite.Close ();
bwread.Close ();
fsread.Close ();
File.Delete (source);
}
using(var inputFile = new FileStream(
"oldFile.txt",
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
{
using (var outputFile = new FileStream("newFile.txt", FileMode.Create))
{
var buffer = new byte[0x10000];
int bytes;
while ((bytes = inputFile.Read(buffer, 0, buffer.Length)) > 0)
{
outputFile.Write(buffer, 0, bytes);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment