Setting Encoding in CSVHelper

Viewed 6805

I am trying to set the encoding to shift_jis. I used the Encoding.GetEncoding(932) but it is not using the encoding when writing the file.

I am setting the encoding value using the Windows identifier 932 on the StreamWriter instantiation of the TextWriter. I see that the encoding value is set to shift_jis. When instantiating the the CsvWriter using the TextWriter object the encoding is getting lost. The encoding value on the CsvWriter is UTF8. See my code below how I am setting the values.

using (TextWriter textWriter = new StreamWriter(fileString, false, 
                                              Encoding.GetEncoding(932)))
{ 
   var writer = new CsvWriter(textWriter, false);
   writer.Configuration.ShouldQuote = (field, context) => false;
   writer.Configuration.Delimiter = ",";

   .. write the columns 
   .. write the rows in a foreach loop

}

When I expand the Encoding properties on the textWriter it is set to shift_jis:

CodePage = 932
EncodingName = "Japanese (Shift-JIS)"

When I expand the Configuration/Encoding properties on the writer you it is set to UTF8:

CodePage = 65001
EncodingName = "Unicode (UTF-8)"

Could you please point out what I am doing wrong so I can write a file in shift_jis.

0 Answers
Related