Enforce LF line endings with CsvHelper

依然范特西╮ 提交于 2019-12-22 05:16:08

问题


If I have some LF converted (using N++) CSV files, everytime I write data to them using JoshClose's CsvHelper the line endings are back to CRLF.

Since I'm having problems with CLRF ROWTERMINATORS in SQL Server, I whish to keep my line endings like the initital status of the file.

Couldn't find it in the culture settings, I compile my own version of the library.

How to proceed?


回答1:


From what I can tell, the line terminator isn't controlled by CvsHelper. I've gotten it to work by adjusting the File writer I pass to CsvWriter.

TextWriter tw = File.CreateText(filepathname);
tw.NewLine = "\n";
CsvWriter csvw = new CsvWriter(tw);
csvw.WriteRecords(records);
csvw.Dispose();



回答2:


Might be useful for somebody:

    public static void AppendToCsv(ShopDataModel shopRecord)
    {
        using (var writer = new StreamWriter(DestinationFile, true))
        {
            using (var csv = new CsvWriter(writer))
            {
                csv.WriteRecord(shopRecord);
                writer.Write("\n");
            }
        }
    }


来源:https://stackoverflow.com/questions/28420135/enforce-lf-line-endings-with-csvhelper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!