Tuesday, 4 May 2010

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

Problem:

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

Solution:

Use encoding "Windows-1252".

Sample code -

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

No comments:

Post a Comment

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