Handling encoding correctly when reading or writing text files in .NET is critical for ensuring data integrity, especially with non-ASCII characters or cross-platform compatibility.
✅ Key Points on Handling Encoding
1. Specify Encoding Explicitly
Always specify the encoding when reading or writing, unless you're sure the default (UTF-8 without BOM) works for your scenario.
Writing Text with Encoding
Using File.WriteAllText:
string text = "Hello, world — 你好,世界";
File.WriteAllText("file.txt", text, Encoding.UTF8); // or Encoding.Unicode, etc.
Using StreamWriter:
using (var writer = new StreamWriter("file.txt", false, Encoding.UTF8))
{
writer.WriteLine("Some text");
}
Common Encodings:
Encoding.UTF8 – Universal, default for many apps
Encoding.Unicode – UTF-16 LE
Encoding.ASCII – 7-bit ASCII only (loses non-ASCII characters)
Encoding.GetEncoding("ISO-8859-1") – Western European (legacy)
new UTF8Encoding(encoderShouldEmitUTF8Identifier: true) – With BOM
using (var reader = new StreamReader("file.txt", Encoding.UTF8))
{
string content = reader.ReadToEnd();
}
Detecting Encoding Automatically (BOM-based):
using (var reader = new StreamReader("file.txt", detectEncodingFromByteOrderMarks: true))
{
string content = reader.ReadToEnd();
}
If you're unsure about the encoding, setting detectEncodingFromByteOrderMarks: true can help detect UTF-8/UTF-16 with BOM.
Example: Writing and Reading Unicode Text
string text = "Text with emoji and Chinese 中文";
// Write with UTF-8
File.WriteAllText("unicode.txt", text, Encoding.UTF8);
// Read it back
string readBack = File.ReadAllText("unicode.txt", Encoding.UTF8);
Common Mistakes
Mistake
Problem
Not specifying encoding
May cause unreadable characters on other systems or in other programs
Using Encoding.ASCII for Unicode text
Non-ASCII characters get lost or replaced
Writing UTF-8 text without BOM when BOM is expected (e.g., in Notepad)
File may appear garbled or empty
Reading UTF-16 with UTF-8
Results in mojibake (garbled text)
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Handling encoding correctly when reading or writing text files in .NET is critical for ensuring data integrity, especially with non-ASCII characters or cross-platform compatibility.
✅ Key Points on Handling Encoding
1. Specify Encoding Explicitly
Always specify the encoding when reading or writing, unless you're sure the default (
UTF-8 without BOM) works for your scenario.Writing Text with Encoding
Using
File.WriteAllText:Using
StreamWriter:Common Encodings:
Encoding.UTF8– Universal, default for many appsEncoding.Unicode– UTF-16 LEEncoding.ASCII– 7-bit ASCII only (loses non-ASCII characters)Encoding.GetEncoding("ISO-8859-1")– Western European (legacy)new UTF8Encoding(encoderShouldEmitUTF8Identifier: true)– With BOMReading Text with Encoding
Using
File.ReadAllText:Using
StreamReader:Detecting Encoding Automatically (BOM-based):
Example: Writing and Reading Unicode Text
Common Mistakes
Encoding.ASCIIfor Unicode text