Alex Rivera | Logout

C# - How do I read and write a binary file?

Asked 2009-09-20T07:45:35.553
11

How do I read a raw byte array from any file, and write that byte array back into a new file?

Edit
Report

1 Answer

7

(edit: note that the question changed; it didn't mention byte[] initially; see revision 1)

Well, File.Copy leaps to mind; but otherwise this sounds like a Stream scenario:

    using (Stream source = File.OpenRead(inPath))
    using (Stream dest = File.Create(outPath)) {
        byte[] buffer = new byte[2048]; // pick size
        int bytesRead;
        while((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) {
            dest.Write(buffer, 0, bytesRead);
        }
    }
answered 2009-09-20T07:59:02.823

Your Answer