I need to read from the same stream twice. The problem is i get a different string the second time.
Here is the shortened code:
using(StreamReader sr = new StreamReader(filePath))
{
string string1 = sr.ReadToEnd();
sr.BaseStream.Position = 0;
sr.DiscardBufferedData();
string string2 = sr.ReadToEnd();
bool sameSame = string1 == string2; // false!
}
The strings look the same but checking with a hex editor i can see that the values are different. What is causing this and how do i get around it?
UPDATE
string1

string2

For some reason string1 has a ‘–‘.
It’s a problem with the byte order mark, a dirty workaround would be to skip the first three bytes:
But the proper way is to create a new StreamReader.