Saturday 5 May 2012

Beginning .Net , C# Tips : Encodings in StreamReader

The StreamReader by default attempts to determine the encoding format of the file. If one of the supported encodings such as UTF-8 or UNICODE is detected, it is used. If the encoding is not recognized,the default encoding of UTF-8 is used. Depending on the constructor you call, you can change the default encoding used and optionally turn off encoding detection.

Below syntax shows how you can control the encoding that the StreamReader uses.
Syntax :
StreamReader reader = new StreamReader(MapPath(TextFile.txt"),System.Text.Encoding.Unicode);

The default encoding for the StreamWriter is also UTF-8, and you can override it in the same manner as with the StreamReader class.

No comments:

Post a Comment