Search code examples
c#encodingutf-8byte-order-mark

UTF8 without BOM ends up in ANSI


With the code like below I ended it up with ANSI encoding. any ideas?

Encoding utf8WithoutBom = new UTF8Encoding(false);
using (FileStream fs = new FileStream("c:\\text.txt", FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
{
    StreamWriter sw = new StreamWriter(fs, utf8WithoutBom);
    sw.Write("sfsdfsdfsdf");
    sw.Flush();
    sw.Close();
    fs.Close();
}

Solution

  • Plain text files do not save what encoding they're in anywhere, there's no format specification for where that meta information should be stored. All you have in the end is a file with a bunch of bytes in it. It's up to any application how it interprets those bytes and how it finds out what encoding the file is in.

    UTF-8 with ASCII-only characters is exactly identical to an ASCII encoded file is exactly identical to an ANSI encoded file or "Latin-1" encoded file. There's no difference in the raw bytes. If an application identifies that file as ANSI that's just as valid as any of the other answers.