How to write out a text file in C# with a code page other than utf-8?

[Originally Posted By]: http://stackoverflow.com/questions/373365/how-to-write-out-a-text-file-in-c-sharp-with-a-code-page-other-than-utf-8

using System.IO;
using System.Text;

using (StreamWriter sw = new StreamWriter(File.Open(myfilename, FileMode.Create), Encoding.WhateverYouWant))
{    
    sw.WriteLine("my text...");     
}

An alternate way of getting your encoding:

using System.IO;
using System.Text;

using (var sw  = new StreamWriter(File.Open(@"c:\myfile.txt", FileMode.CreateNew), Encoding.GetEncoding("iso-8859-1"))) {
    sw.WriteLine("my text..."); 			
}

Check out the docs for the StreamWriter constructor.

shareedit