.NET Framework Convert text file encoding


Text is saved encoded (see also Strings topic) then sometimes you may need to change its encoding, this example assumes (for simplicity) that file is not too big and it can be entirely read in memory:

public static void ConvertEncoding(string path, Encoding from, Encoding to)
    File.WriteAllText(path, File.ReadAllText(path, from), to);

When performing conversions do not forget that file may contain BOM (Byte Order Mark), to better understand how it's managed refer to Encoding.UTF8.GetString doesn't take into account the Preamble/BOM.