Why are you doing that?
All you are doing is slowing the whole thing down horribly - because you have to allocate a new array each time you write, and you destroy all previous data in the file: that;s what WriteAllBytes does!
Quote:
Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
File.WriteAllBytes Method (String, Byte[]) (System.IO)[
^]
Since your block size is 4096 bytes, that means you allocate over 2000 new arrays in order to do this!
First off, for .NET, 9MB is not that large a file: a single WriteAllBytes with a 9MB array will be as quick as the file system can cope with (and that's pretty fast, it knows a lot more about block sizes and caches than you do!).
Second if you want to write in chunks, then open a single stream outside the loop, and use
FileStream.Write Method (Byte[], Int32, Int32) (System.IO)[
^] to output each chunk - it provides offset and length parameters for just that purpose without needing to allocate memory for each chunk. But ... it'll still be slower than writing it all at once!