Tuesday, 4 May 2010

Invalid characters (square boxes) while reading a text file (encoding issue)


While reading a text file we get square symbols in .NET for special symbols. For e.g ° .
However if the file is created\saved as Unicode then the file is read correctly.

- tried encoding.UTF-8 but still get the error string
- notepad opens up the file correctly with the special characters


Use encoding "Windows-1252".

Sample code -

using (StreamReader reader = new StreamReader(filePath, System.Text.Encoding.GetEncoding("Windows-1252")))
string content = reader.ReadToEnd();
return content;

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

C# Reactive Extensions - Buffer and Window

I was going through Buffer and Window in RX, thought a few examples would help clear the differences. First create a buffer of even numbe...