-2

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);
George
  • 124
  • 1
  • 12
  • 1
    Since this library probably uses a StreamWriter / TextWriter derived class, you could initialize your own and set the `TextWriter.NewLine` to something else than the default `"\r\n"` in Windows -- Not clear why you would replace both Carriage Return and Line Feed, though. A text editor would show a single line of everything you have stored. Reading back would also be problematic. What problem are you trying to solve? – Jimi Jan 10 '23 at 07:34
  • Hi, thx for the comment. Yeah like you said, this doesn't make sense, true. I just need to get rid of the last one like I marked on the image, because the Iot modul detects the Carriage Return and won't connect to the WiFi. – George Jan 10 '23 at 07:47
  • You should send the package compressed (GZipStream or similar or a compressed file or a base64 string). Stripping the line terminators is bound to cause grief -- Not clear what causes a problem, though – Jimi Jan 10 '23 at 07:50
  • 1
    The newline is "end of record" so without it, the last record, and therefore the file, is incomplete. You really *don't* want to omit it. – madreflection Jan 10 '23 at 07:56

1 Answers1

-1

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())); 
} 
George
  • 124
  • 1
  • 12