Some of our new users to managed code take a little time to get used to the design pattern in reading a file. I thought I’ll enumerate through the most common patterns for their benefit;
String fileName =...;
using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
int size = 4096;
Byte bits = new Byte[size];
while ((count = stream.Read(bits, 0, size)) != 0)
//bits buffer has valid data from 0 to count-1 that can be processed here
Reading from a text file: The StreamReader has some convenient methods to read from it, where ReadLine() is the most popular;
String fileName =...
using (StreamReader reader = new StreamReader(fileName))
while ((line = reader.ReadLine()) != null)
//process the line
StreamReader also has a method to read the whole file, ReadToEnd(), but it has some gotchas which the documentation covers well. You can also use Read(Char, int, int) similar to FileStream.Read method above if you want to work with characters.