---
title: "System.Text.Encoding.Tip"
url: "https://osherove.com/blog/2003/5/15/systemtextencodingtip.md"
canonical: "https://osherove.com/blog/2003/5/15/systemtextencodingtip.html"
published: "2003-05-15T08:46:00"
updated: "2003-05-15T08:46:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "I learned this the hard way, so you don't have to. This applies to anyone who is going to be using System.IO to read ANSI files that combine Hebrew And English mixed inside. Now, when I attempted to do this, I used the…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# System.Text.Encoding.Tip

By Roy Osherove May 15, 2003 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published May 15, 2003; technical details may be outdated.

I learned this the hard way, so you don't have to. This applies to anyone who is going to be using System.IO to read ANSI files that combine Hebrew **And** English mixed inside.

Now, when I attempted to do this, I used the StreamReader class, which is pretty simple to use:

StreamReader r = new StreamReader(myFile); string MyText = r.ReadToEnd(); r.Close();

Ah! but there's one important caveat you'll notice when you try to MyText. The hebrew string inside will vanish without a trace, leaving you with a big heart attack...

To fix this problem, you'll need to specify the encoding in which the file is formatted. This is done by specifying one of the **Encoding** Classes available through **System.Text.Encoding.\***  like so:

StreamReader

r = new StreamReader(myFile,System.Text.Encoding.Default);

What I found is that passing any other encoding type does not work for these files , and either truncates the text or displays garbage.  Since Encoding.Default automatically gets the encoding with the same codepage used by your system it saves you from the trouble of trying to figure this stuff out. I have no borderline cases to check against, but it seems to work perfectly for me.

Conclusion: when  reading an ANSI file, or if you're having problem reading any text file format, first try to read it by passing in Encoding.Default, and only then all the other types.

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- May 19, 2003

    ### [Articles, Articles, Articles!](https://osherove.com/blog/2003/5/19/articles-articles-articles.html)

- May 17, 2003

    ### [UI Threading Helper Classes](https://osherove.com/blog/2003/5/17/ui-threading-helper-classes-1.html)

- May 14, 2003

    ### [A Long Day!](https://osherove.com/blog/2003/5/14/a-long-day.html)

- May 13, 2003

    ### [Strip HTML tags from a string using regular expressions](https://osherove.com/blog/2003/5/13/strip-html-tags-from-a-string-using-regular-expressions.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
