Simply read with
System.IO.StreamReader reader =
new System.IO.StreamReader(fileName, System.Text.Encoding.ASCII);
or, more universally, auto-detect the encoding:
System.IO.StreamReader reader =
new System.IO.StreamReader(fileName, true);
It will give you Unicode string(s) based on your ASCII data. In principle, this is all you need. You can write it back with
bool appendOrNot =
System.IO.StreamWriter writer =
new System.IO.StreamWriter(fileName, appendOrNot, System.Text.Encoding.UTF8);
As you text data is, generally speaking, always Unicode, prefer using on output only one of Unicode UTFs. The only text encoding supported in character and string data internally is UTF-16. All other encodings are only supported as persistence; they are represented in memory as arrays of bytes, with no regards to characters boundaries, which can vary (in UTF-8, character size is 1-4 bytes, in UTF-16 — one or two 16-bit words (two words called
surrogate pair, in UTF-32 — always one 32-bit word). Please see two very last links above.
Please see:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[
^],
http://msdn.microsoft.com/en-us/library/f5f5x7kt.aspx[
^];
http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[
^];
you also need to understand how Unicode and BOM work:
http://unicode.org/[
^],
http://unicode.org/faq/utf_bom.html[
^].
BOM (or its absence) is used for auto-detection of encoding mentioned above.
[EDIT]
Apparently, auto-detecting of the encoding by BOM is needed only in one case: if the encoding is some Unicode UTF, you know what encoding is that, but BOM is not present. Such things happen. This is also explained in the last Unicode article referenced above.
—SA