Just Code‎ > ‎

C# - Convert File to another Encoding.

posted Feb 12, 2010, 6:15 AM by Peter Henell
public void convertToOldEncoding(string fileName, Encoding encoding)
        {
            string tempFile = fileName + ".tmp";

            try
            {
                if (File.Exists(tempFile)) File.Delete(tempFile);

                File.Move(fileName, tempFile);

                StreamReader sr = new StreamReader(tempFile);
                StreamWriter sw = new StreamWriter(fileName, false, encoding);

                sw.WriteLine(sr.ReadToEnd());

                sw.Close();
                sr.Close();

                File.Delete(tempFile);

                Alarm.AlarmInstance.writeToLog("File converted to Ascii: " + fileName);

            }
            catch (Exception ex)
            {
                Alarm.AlarmInstance.raiseAlarm("convertToOldEncoding failed with message: " + ex.ToString());
            }
            
        }
Comments