Convert text file encoding
suggest changeText files are stored in a particular encoding (see also the Strings topic). You may later need to change that encoding. This example assumes, for simplicity, that the file is small enough to read entirely into memory:
public static void ConvertEncoding(string path, Encoding from, Encoding to)
{
File.WriteAllText(path, File.ReadAllText(path, from), to);
}
A file may begin with a BOM (byte order mark). File.ReadAllText skips a preamble it recognizes, and File.WriteAllText writes one when the target encoding defines a preamble. Encoding.GetString does not strip a preamble; see Encoding.UTF8.GetString doesn’t take into account the Preamble/BOM.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents