Hmm. Thanks for the details. Very interesting.
Two questions:
1. Does ColinR's sample work as a workaround?
2. Does your example work with files on Windows (i.e. is the behavior inconsistent between .NET and .NET MF)?
Chris
Very interesting. I wonder if that has become the recommended way to check for EndOfStream? If you search you'll find that a lot of people give examples they way I use EndOfStream. I managed to find one official example as well: http://msdn.microsof...y/ee461504.aspx (step 9).
Answers:-
2. This design pattern does work in the standard framework as I use it quite often.
1. Interesting, this pattern manages to read all the lines, but doesn't stop trying to read lines when the EndOfStream is hit:-
Debug.Print(File.Exists(filename) ? "File Found" : "File Not Found!"); using (StreamReader sr = new StreamReader(filename)) { Debug.Print("Initial EOS:" + sr.EndOfStream.ToString()); String line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { Debug.Print("LINE:" + line + " - EOS:" + sr.EndOfStream.ToString()); } }
This gived me the following output...
File Found
Initial EOS:True
LINE:TestLine0 - EOS:False
LINE:TestLine1 - EOS:False
LINE:TestLine2 - EOS:False
LINE:TestLine3 - EOS:False
LINE:TestLine4 - EOS:True
LINE: - EOS:True
LINE: - EOS:True
LINE: - EOS:True
...
So it looks like EndOfStream isn't set until the first line is read.
If I step over the code, "line" is never set to null, if there's no data available it seems to be set to empty string. I wouldn't be happy to use this to determin the end of file.
It looks like I can get what I want by using a mix of this approach and adding a check for EndOfStream within the loop, like this:-
using (StreamReader sr = new StreamReader(filename)) { Debug.Print("Initial EOS:" + sr.EndOfStream.ToString()); String line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { Debug.Print("LINE:" + line + " - EOS:" + sr.EndOfStream.ToString()); if (sr.EndOfStream) break; } }
Tim