I'm getting files which can be latin1 or utf8 encoding. I Get it as a stream in C#. How can I detect if its latin1 ("ISO-8859-1") or UTF-8? When I try to detect it, it will always detect it as UTF-8. This code don't work, if will always be UTF-8.
private Encoding GetUtf8EncodeStream(Stream fileStream)
{
using var reader = new StreamReader(fileStream, true);
var encoding = reader.CurrentEncoding;
if (Equals(encoding, Encoding.UTF8))
{
return Encoding.UTF8;
}
return Encoding.GetEncoding("ISO-8859-1");
}
void Method(){
var encoding = GetUtf8EncodeStream(fileStream);
using (TextReader reader = new StreamReader(fileStream, encoding))
}
I first need to know the encoding, and then I will read it with that encoding.
I need to know the encoding, because it has special characters æ, ø and å. And if i try to read a stream, which has encoding: latin1 and set the streamreader to UTF-8, there will be question marks instead of the characters. And if I do it reversed where I set the StreamWriter to encoding UTF-8, and its in latin1 the hell will break lose ;)