Another short one...how to read the contents of an entire file into a Byte Array... (English)

Another short one...how to read the contents of an entire file into a Byte Array...

Sunday, 04 July 2004

//

Less than a minute

This just another one of thoise dumb little snippets that I always forget and is a bit of a bugger to find...so, if you for some reason (e.g., compression / encryption) need all of the contents of a file to be held in a byte array, here's the simplest way I've found to do it:

byte[] inArr; using(Stream s=File.Open("myfile.dat",FileMode.Open)) { inArr = new byte[s.Length]; s.Read(inArr,0,inArr.Length); }

logo

© 2025 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.