Proposed exercise
Create a program to copy a source file into a destination file. You must use FileStream and a block size of 512 Kb. An example of use might be:
mycopy file.txt e:\file2.txt
It must behave correctly if the source file does not exist and it must warn (but not overwrite it) if the destination file exists.
Solution
using System;
using System.IO;
public class FileCopier
{
public static void Main()
{
const int BUFFER_SIZE = 512 * 1024;
byte[] data = new byte[BUFFER_SIZE];
FileStream fileR = File.OpenRead("data.exe");
FileStream fileW = File.Create("data.copy.exe");
int amountRead;
do
{
amountRead = fileR.Read( data,0,BUFFER_SIZE );
fileW.Write( data,0,amountRead );
}
while (amountRead == BUFFER_SIZE);
fileR.Close();
fileW.Close();
}
}