Search code examples
c#formscsvlinqtocsv

Writting to CSV file using LINQtoCSV, puts CR LF at the end of the file


I'm writting wifi data (ID and password) into a CSV file using the LINQtoCSV NuGet and I'm getting at the end of the line Carriage return (red ractangle on the picture).

enter image description here

Could the .TrimEnd('\r', '\n') method help me in some way?

My code:

var listSettings = new List<Wifi_User>
{
    new Wifi_User
    {
        SSID = "id_112",
        password = "guest"
    }
};

var csvFileDescription = new CsvFileDescription
{
    FirstLineHasColumnNames = true,
    SeparatorChar = ','
};

var csvContext = new CsvContext();
csvContext.Write(listSettings,"D:\\WIFICRED.CSV",csvFileDescription);

Solution

  • Found a solution... If there is a CR/LF at the end of the line I'm using this syntax...

    string myFileData;
    myFileData = File.ReadAllText(@"D:\CsvFile.csv");
    if (myFileData.EndsWith(Environment.NewLine))
    {
        File.WriteAllText(@"D:\CsvFile.csv", myFileData.TrimEnd(Environment.NewLine.ToCharArray())); 
    }