57

I want to write out a text file.

Instead of the default UTF-8, I want to write it encoded as ISO-8859-1 which is code page 28591. I have no idea how to do this...

I'm writing out my file with the following very simple code:

using (StreamWriter sw = File.CreateText(myfilename))
{
    sw.WriteLine("my text...");
    sw.Close();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
adeena
  • 4,027
  • 15
  • 40
  • 52

5 Answers5

92
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.

Dave Markle
  • 95,573
  • 20
  • 147
  • 170
  • 1
    Can "whatever I want" be a code page number or...? the autocomplete on the function in MS Visual C# isn't giving me an "iso-8859-1" option... just the UTF8, UTF16, etc... – adeena Dec 17 '08 at 01:14
  • Encoding.GetEncoding("iso-8859-1") – frenchone Aug 31 '15 at 08:02
  • What's wrong with just: `using (StreamWriter sw = new StreamWriter(File.Create(myfilename), Encoding.WhateverYouWant)) { }` – Dan W Jan 17 '19 at 02:45
35

Simple!

System.IO.File.WriteAllText(path, text, Encoding.GetEncoding(28591));
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
  • 4
    It can be even more simple: File.WriteAllLines(path, lines, Encoding.UTF8); – Lev Z Apr 22 '18 at 13:17
  • 2
    @LevZ: Excellent addition - I suspect it wasn't available when I wrote the answer 9 years ago :) – Johann Gerell Apr 23 '18 at 07:46
  • @LevZ: `UTF-8` may not always work, and you might need to use a specific 8-bit ASCII encoding. `UTF-8` is liable to save the file as UNICODE, so best to check carefully. – AlainD May 06 '22 at 12:46
  • @AlainD please clarify what you mean with "may not always work" – Johann Gerell May 09 '22 at 06:29
  • Load any 8-bit file saved with ISO-8859-X encoding using `File.ReadAllText`. Write back to disk with `File.WriteAllText`. ASCII characters > 127 are liable to be changed (example: `Š`). File size changes if the original had strictly 8-bit ASCII encoding. One of my example files went from 133kB to 201kB and BEFORE != AFTER. The point is, if reading a file know how the original file was created, and know how the output file will be consumed. `28591` and `UTF-8` are good, but may be inappropriate in some situations. – AlainD May 09 '22 at 09:58
4

Wrap your StreamWriter with FileStream, this way:

string fileName = "test.txt";
string textToAdd = "Example text in file";
Encoding encoding = Encoding.GetEncoding("ISO-8859-1"); //Or any other Encoding

using (FileStream fs = new FileStream(fileName, FileMode.CreateNew))
{
    using (StreamWriter writer = new StreamWriter(fs, encoding))
    {
        writer.Write(textToAdd);
    }
}

Look at MSDN

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tree
  • 41
  • 1
0

You can have something like this

 switch (EncodingFormat.Trim().ToLower())
    {
        case "utf-8":
            File.WriteAllBytes(fileName, ASCIIEncoding.Convert(ASCIIEncoding.ASCII, new UTF8Encoding(false), convertToCSV(result, fileName)));
            break;
        case "utf-8+bom":
            File.WriteAllBytes(fileName, ASCIIEncoding.Convert(ASCIIEncoding.ASCII, new UTF8Encoding(true), convertToCSV(result, fileName)));
            break;
        case "ISO-8859-1":
            File.WriteAllBytes(fileName, ASCIIEncoding.Convert(ASCIIEncoding.ASCII, Encoding.GetEncoding("iso-8859-1"), convertToCSV(result, fileName)));
            break;
        case ..............
    }
Manish Joisar
  • 1,256
  • 3
  • 23
  • 47
-5

Change the Encoding of the stream writer. It's a property.

http://msdn.microsoft.com/en-us/library/system.io.streamwriter.encoding.aspx

So:

sw.Encoding = Encoding.GetEncoding(28591);

Prior to writing to the stream.

Steven Behnke
  • 3,336
  • 3
  • 26
  • 34