Mukil Deepthi
Mukil Deepthi

Reputation: 6452

C# CSVWriter how to avoid getting empty line at the end of writing all lines to a file

I am using CSVWriter to write to csv by using the following link:

How to create csv with header and footer using LinqtoCsv or anything better in C#

But for some reason i am getting an empty line created at the end. Can anyone help me what am i doing wrong or how can i avoid getting an empty line?

Here is the code to create csv using csvwriter:

        using (var writer = File.AppendText(outputCsv))
        {
            var csv = new CsvWriter(writer);
            csv.Configuration.RegisterClassMap<CustomMap>();
            csv.Configuration.HasHeaderRecord = true;
            csv.Configuration.QuoteNoFields = true;
            csv.Configuration.Delimiter = "|";

            csv.WriteRecords(asRunLogs.ToList());
            csv.WriteRecord(new CSVFooterRecord()
            {
                FooterText =
                    $"TRAILER:{"qsaam_promos.csv"}|{DateTime.Now.ToString("ddMMyyHHmm")}|{asRunLogs.Count}"
            });
        }

When i open the file in Notepad++, i can see an empty line at the end.

Appreciate help with regard to this.

Thanks

Upvotes: 1

Views: 1613

Answers (1)

Alex Zhmerik
Alex Zhmerik

Reputation: 365

You could do

writer.Write($"TRAILER:{"qsaam_promos.csv"}|{DateTime.Now.ToString("ddMMyyHHmm")}|{asRunLogs.Count}");
writer.Flush();

instead of

csv.WriteRecord(new CSVFooterRecord()
{
    FooterText =
        $"TRAILER:{"qsaam_promos.csv"}|{DateTime.Now.ToString("ddMMyyHHmm")}|{asRunLogs.Count}"
});

Upvotes: 2

Related Questions